Reputation: 23
I have following content of file text.txt
bla,one,bla
bla,two,bla
bla,one,bla
bla,one,bla
I would like to key off 2nd field and split content into following files:
# One.txt
bla,one,bla
bla,one,bla
bla,one,bla
And
#Two.txt:
bla,two,bla
Upvotes: 0
Views: 50
Reputation: 26667
This can be very easily done using redirections within an awk
script
awk -F, '{print > ($2".txt")}'
Example
$ awk k -F, '{print > ($2".txt")}' file
$ cat one.txt
bla,one,bla
bla,one,bla
bla,one,bla
$ cat two.txt
bla,two,bla
print
by default prints the current input line.
> $2".txt"
Redirects the print output to the a file with second column as its name.
It would be a good idea to close the files as well, so that we don't end up with too many opened file descriptors, Just in case the input file is too big.
close($2."txt")
Upvotes: 3