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!

Making multiple output file from a single data set 2

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I have a large dataset, I want to make multiple output file after reading every 4th row. For example after reading four (4) rows will have one dataset, such as 1st.out, 2nd.out, 3rd.out, etc.
Is there any single script which will do this. I can run awk multiple times to create each output dataset, however, this seems to be tedious.

Attached is an example dataset:
0.000116666 0.000116666 0.000116666 0.000125 0.000133333 0.000149999 0.000324999 0.000649997 0.000624998 0.000491665
0.000274999 0.000149999 0.000149999 0.000158333 0.000158333 0.000208333 0.000358332 0.000366665 0.000374998 0.000383332
0.000383332 0.000399998 0.000399998 0.000383332 0.000191666 0.000191666 0.000166666 0.000141666 0.000141666 0.000141666
0.000141666 0.000141666 0.000183333 0.000224999 0.000224999 0.000158333 0.000158333 0.000174999 0.000241666 0.000241666
0.000241666 0.000241666 0.000233332 0.000233332 0.000233332 0.000224999 0.000224999 0.000224999 0.000791664 0.00095833
0.000941663 0.000358332 0.000166666 0.000158333 0.000133333 0.000133333 0.000149999 0.000166666 0.000324999 0.000408332
0.000391665 0.000366665 0.000349999 0.000341665 0.000324999 0.000316665 0.000316665 0.000308332 0.000291666 0.000283332
0.000333332 0.000433332 0.000449998 0.000416665 0.000299999 0.000208333 0.000158333 0.000166666 0.000108333 3.33332e-005

Thanks.
 
Something like this ?
awk 'BEGIN{out="1.out";i=1}
{print>out}
(NR%4)==0{close(out);++i;out=i".out"}
' /path/to/inputfile

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Code:
BEGIN { makename() }
# Print only non-blank lines.
$1 {print >out}
NR%4==0 { close(out); makename() }
function makename()
{ out=sprintf( "data%06d.out", ++filecount)
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top