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!

Breaking a large file after each interval into multiple files 1

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I have a large dataset, where I want to break the file into smaller files after certain number of rows.
Example, my dataset looks like:

G-1222
1 OBSERVED
0.0000 0.0000
583 COMPUTED
1 3
7 9
15 12
G-1736
1 OBSERVED
0.0012 0.000
583 COMPUTED
5 7
3 6
6 8

The final output will be in the form of multiple output, in this above example 2 outputs

output1.dat

G-1222
1 OBSERVED
0.0000 0.0000
583 COMPUTED
1 3
7 9
15 12


output2.dat

G-1736
1 OBSERVED
0.0012 0.000
583 COMPUTED
5 7
3 6
6 8

Thus in the above example case, every 7 rows the dataset was broken into multiple file.
Any help appreciated. Thanks.
 
Something like this ?
awk '
BEGIN{i=1}
{print > "output"i".dat"}
!(NR%7){close("output"i".dat");++i}
' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
BEGIN { rows = 7 }

NR%rows == 1 { makefilename() }
{ print >filename }

function makefilename()
{ if (filename)
    close(filename)
  n = int( NR / rows ) + 1
  filename = "output" sprintf("%04d",n) ".dat"
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top