Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract sections from file

Status
Not open for further replies.

stackdump

Technical User
Sep 21, 2004
278
GB
I have a file with the contents of;

Section A
fsfs
ss
gsgs
End A

Section B
ds
ssf
End B
etc. etc.

I want to generate a file for each section, so a file called "A" and "B" etc. In each file, the text for that section. The number of lines between section start and end are variable.

Any easy way to do this?
 
try csplit command (if your unix has it)

csplit "/^Section /" "*" </path/to/inputfile

But if I read man page (which you should do also ;-)) correctly, the section files will be named 000, 001, 002, ...


HTH,

p5wizard
 
Thanks.

I got a solution running with awk, for posterity, here it is...

{
if (match($0,"SECTION"))
{
InSection= 1
FileName = $2".txt"
}
if (match($0,"END SECTION"))
{
InSection=0
print > FileName
next
}
if (InSection) print > FileName
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top