Dharmendra
Dharmendra

Reputation: 1

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 clearly and simply.
If your question is about this website, ask it on meta instead.

Upvotes: 0

Views: 126

Answers (3)

pavium
pavium

Reputation: 15118

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

Upvotes: 2

Dennis Williamson
Dennis Williamson

Reputation: 359955

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

Upvotes: 2

ghostdog74
ghostdog74

Reputation: 342323

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

Upvotes: 1

Related Questions