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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting file

Status
Not open for further replies.

chalcopyrites

Technical User
Mar 11, 2004
12
GB
Hi everyone,

I have an input file like this:

aaaaaa 8493572 384276 2348973 2453
aaaaaa 490652 903468209685 290862568 80936
aaaaaa 9856 23948573429857 39485723495873 93482
aaaaaa 83753958 84571395 34982573045 19837
bbbbbb 89347295 23497389 39824705 389427904
bbbbbb 490652 903468209685 290862568 80936
bbbbbb 9856 23948573429857 39485723495873 93482
cccccc 11111 11111 11111 11111
cccccc 39425234 8976096 759867 20348957
cccccc 3490781205 38947623 73849 893457
cccccc 048 0348q50 3984257 234908574

and would like to separate it into 3 files based on the first field.

I have got the following (cobbled together from various other bits and pieces):

BEGIN {
i=0
filename = "file"i".dat"
}
{
if (hn!=$1){
i += 1
if (filename != "") close (filename);
filename = "file"i".dat";
print "Creating File : "i
getline
}
hn=$1
}
{
print $0 > filename
}

which splits the input into 3 files based on the first field but it always misses off the first line in each sequence.

Can you tell me where I am going wrong? Awk scripts aren't my strong point!

Thanks

Erica
 
How about using the command "csplit"? That would do a good job without you having to write so much code.
man csplit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top