write lines of one file to new file dependent on first field of line

write lines of one file to new file dependent on first field of line

Post by Janis Papa » Thu, 10 Jul 2008 07:28:53



Are all records with first field equal appearing as a block as in the
example data or will they be spread across the file?

How many different values can appear in field 1? In case there are only
a few it can be as simple as...

awk '{print > $1}'

If the number of different values for field 1 exceeds the number of
simultaneous open filedescriptors then it's more complex...

awk '$1 != s1 {close ($1)} {print > $1 ; s1 = $1}'

In the most general case you have to close/append for each record (which
is likely to be the slowest of the three solutions)...

awk '{print >> $1 ; close($1)}'


Janis