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!

separate

Status
Not open for further replies.

ovince

Programmer
Feb 27, 2007
55
FR
hi All

what is the easyest way (if posible with one command line) to separate large files into subfiles starting form one header to another. For example,

#head 1
23434
23434
34324
#head 2
345345
#head 3
34543
34545
34545
34545

to separate into

#head
23434
23434
34324

#head
345345

and

#head
34543
34545
34545
34545

respectively.

Number of rows betwwen headers are not equal. Headers have same name

thanks
oliver

 
csplit can do that as well, but first you need to find out how many "#head"s and how many digits to use for the individual file numbers...


Code:
heads=$(grep -c "^#head" /path/to/file)

((heads=heads-1))

digits=$(expr length $heads)

csplit -n $digits -f head /path/to/file "/^#head/" "{$heads}"



HTH,

p5wizard
 
thank you all for reply. Feherke has a very elegant solution with awk and it works. Thanks Feherke (koszonom)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top