Поскольку речь идет о сотнях мегабайт данных, возможно, стоит использовать опции -o
и -g
gnu ls, чтобы избежать печати пользователя и группы, в результате чего получится такой формат:
-rw-rw-r-- 1 952K Oct 31 01:13 a001.jpg
Эта команда sed удалит ненужные данные в начале строки:
sed 's/^[-a-z]{10} \{1,\}[0-9]\{1,\}//'
Вы можете объединить листинг и удаление ненужных данных в один шаг (это также относится к большинству решений на этой странице), что также может сэкономить вам время:
ls -Rlhog /path/to/directory | sed 's/^[-a-z]\{10\} \{1,\}[0-9]\{1,\}//' > file
Да, спасибо @George Vasiliou, я смог заставить это работать, теперь сценарий работает так:
#!/bin/sh
echo "file to split?"
read file
# This variable is to name resulting files
f=0
while :
do
# Count how many occurrences of "<text" are in the file to split
count=$(grep "<text" "$file" | wc -l)
if [ "$count" -gt 1 ]
then
# Send the occurrences of "<text" with their line number to the titles.txt file
grep -n "<text" "$file" > titles.txt
# From the second line of titles get the line number
lines=$(cat titles.txt| sed -n 2'p' | sed -r 's/^([0-9]*).*/\1/g')
# Every time the script is run the resulting file gets the next number as name
f=$((f+1))
# From the line number obtained at the second line substract 1
substrac="$(($lines-1))"
# Create a new file taking the amount of lines indicated by the substraction from the splitting file
head -"$substrac" "$file" > "$f"
# Delete the lines corresponding to the newly created file from the splitting file to start the process over
sed -i '1,'"$substrac"'d' "$file"
echo "file \"$f\" generated"
else
echo "process finished!"
exit 1;
fi
done
Пояснение:У меня есть огромный текстовый файл такого формата:
<text id="cade2296-1">
many
undetermined
lines
...
</text>
The same schema repeteated undetermined times
<text id="cafr3062-1">
many
undetermined
lines
...
</text>
А мне нужна каждая схема в отдельном файле.
Если файл представляет собой правильно -сформированный XML-файл, и вы хотите извлечь узлы <text>
в отдельные файлы, тогда это делается с помощью XMLStarlet:
#!/bin/sh
infile="$1"
xmlstarlet sel -t -v '//text/@id' -nl "$infile" |
while read id; do
xmlstarlet sel -t --var id="'$id'" -v '//text[@id = $id]' "$infile" >"$id.txt"
done
Учитывая имя пути к файлу, подобное следующему в командной строке,
<?xml version="1.0"?>
<root>
<text id="cade2296-1">
The first text, called "cade2296-1".
</text>
<text id="cafr3062-1">
The second text, called "cafr3062-1".
</text>
</root>
... это создаст два файла, cade2296-1.txt
и cafr3062-1.txt
, в текущем каталоге с содержимым двух тегов <text>
в исходном файле.
Имена файлов берутся из атрибута id
тегов <text>
. Эти значения id
сначала извлекаются из XML, а затем используются в цикле для извлечения соответствующих значений тегов.
Изменение -v
на -c
в вызове XMLStarlet внутри цикла даст вам копию тегов <text>
XML, а не только данные в теге.