Как отфильтровать разные файлы из одного файла - PullRequest
0 голосов
/ 02 декабря 2009

Позвольте мне сказать, что я хочу разные файлы, начиная с "процесса" до следующего "процесса". Например Входной файл

Process=0
We prefer questions that can be answered, not just discussed.
Provide details. Write clearly and simply.
If your question is about this website, ask it on meta instead.

Process=1
We prefer questions that can be answered, not just discussed.
Provide details. Write clearly and simply.
If your question is about this website, ask it on meta instead.
Process=2
We prefer questions that can be answered, not just discussed.
Provide details. Write clearly and simply.
If your question is about this website, ask it on meta instead.

Ожидаемый результат Файл_0 должен содержать

Process=0
We prefer questions that can be answered, not just discussed.
Provide details. Write clearly and simply.
If your question is about this website, ask it on meta instead.

Файл_1 должен содержать

Process=1
We prefer questions that can be answered, not just discussed.
Provide details. Write clearly and simply.
If your question is about this website, ask it on meta instead.

Файл_2 должен содержать

Process=2
We prefer questions that can be answered, not just discussed.
Provide details. Write clearly and simply.
If your question is about this website, ask it on meta instead.

Ответы [ 3 ]

2 голосов
/ 02 декабря 2009

Это создает файлы для каждого раздела и выводит на них текст. Если перед первым «Процессом» есть текст, он помещается в файл с именем «Преамбула».

awk -F '[ =]' 'BEGIN {file="Preamble"} {if ($1 == "Process") file="File_"$2; print >> file}' inputfile
2 голосов
/ 02 декабря 2009

Посмотрите на команду csplit в Linux. Он разбивает текстовые файлы по разделителям (которые могут быть определены регулярным выражением).

1 голос
/ 02 декабря 2009

использовать gawk / nawk (Solaris)

gawk -F"=" '/Process/{f=1;n=$2;print $0 > "File_"n;next}
f && /Process/{f=0} 
f&&NF{print $0 > "File_"n}
' file
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...