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 into multiple file based on first two columns 2

Status
Not open for further replies.

hill007

Technical User
Mar 9, 2004
60
US
I want to break a large file into multiple file based on first two columns:
For example my file is as follows:



87.73,49.62,-45.000000,0,-261581.000000,0.000000,1,1
87.73,49.62,-45.000000,0,-261581.000000,0.000000,2,2
87.73,49.62,-45.000000,0,-261581.000000,0.000000,3,3
87.74,49.63,-45.000000,0,-261581.000000,0.000000,4,4
87.74,49.63,-45.000000,0,-261581.000000,0.000000,5,5
87.74,49.63,-45.000000,0,-261581.000000,0.000000,6,6
87.74,49.63,-45.000000,0,-261581.000000,0.000000,7,7

The Final results should look like:

File 1
87.73,49.62,-45.000000,0,-261581.000000,0.000000,1,1
87.73,49.62,-45.000000,0,-261581.000000,0.000000,2,2
87.73,49.62,-45.000000,0,-261581.000000,0.000000,3,3

File 2
87.74,49.63,-45.000000,0,-261581.000000,0.000000,4,4
87.74,49.63,-45.000000,0,-261581.000000,0.000000,5,5
87.74,49.63,-45.000000,0,-261581.000000,0.000000,6,6
87.74,49.63,-45.000000,0,-261581.000000,0.000000,7,7

Any help will be appreciated.

Thanks.


 
[not tested]

nawk -f hill.awk myFile.txt

Code:
{
   idx= $1 SUBSEP $2
   if ( !(idx in arr) )
      arr[idx] = ++cnt;
   outFile= File arr[idx];
   print $0 >> outFile;
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Tested (assumes input is sorted on first 2 columns).
Code:
BEGIN { FS="," }

f1 != $1 FS $2 {
  if (length($1))
  { f1 = $1 FS $2
    close( filename )
    count++
    filename=sprintf( "outf%05d", count)
  }
}

{ print  >filename }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top