开发者

How to filter different files from one file

Let as say I want different files starting from "process" till next "process. For example Input file

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.

Expected output File_0 should contain

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 should contain

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 should contain

Process=2
We prefer questions that can be answered, not just discussed.
Provide details. Write cl开发者_StackOverflow中文版early and simply.
If your question is about this website, ask it on meta instead.


This creates the files for each section and outputs the text to them. If there is text before the first "Process" then it is put in a file called "Preamble".

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


Look at the csplit command in Linux. It splits text files at delimiters (which may be defined by a regular expression).


use 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜