Split file according to column values

Hi all,
I am trying to split a file by the values of the FIRST column.
The following

awk 

works to split the file by the value of the LAST column -- How can I alter this to divide the column by the FIRST column??

awk -F"\t" '{ print > $NF ; close($NF)}' filename1

Thanks!

Replace $NF with $1

1 Like

any reason to why it only prints out the first line of each different Col1 value?

You should append to file instead because you are closing the file after each write operation:

print >> $1
1 Like

If I recall correctly, even > will append to a file if used in the same awk process. If you run awk several times, any instance should start a new output file with >; append with >>.
BUT: Sure it's the FIRST line per $1 value? Should it overwrite with >, I'd presume the last line is retained.

OR something like this

$ awk -F'\t' '{if(!($NF in F)){ print > $NF } else { print >> $NF} ; F[$NF]; close($NF)}' file