如何从一个文件中过滤不同的文件

时间:2009-12-02 05:58:27

标签: awk

假设我想要从“流程”开始直到下一个“流程”的不同文件。 例如 输入文件

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.

预期产出 File_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.

File_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.

File_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 个答案:

答案 0 :(得分:2)

查看Linux中的csplit命令。它在分隔符处拆分文本文件(可以用正则表达式定义)。

答案 1 :(得分:2)

这将为每个部分创建文件并将文本输出到它们。如果在第一个“处理”之前有文本,则将其放入名为“Preamble”的文件中。

awk -F '[ =]' 'BEGIN {file="Preamble"} {if ($1 == "Process") file="File_"$2; print >> file}' inputfile

答案 2 :(得分:1)

使用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