Create files based on second column of a file

Hi All,

I have a file which looks like this:

234422 1 .00222
323232 1 3232
32323 1 0.00222
1234 2 1211
2332 2 0.9
233 3 0.883
123 3 45

As you can see, the second column of the file is already sorted which I did using sort command.

Now, I want to create files based on the second column. I mean, all the 1s in second column go in one file named 1.tmp and same with 2.tmp and so on until last number of the file

This is what I mean:

1.tmp

234422 1 .00222
323232 1 3232
32323 1 0.00222

2.tmp

1234 2 1211
2332 2 0.9

3.tmp

233 3 0.883
123 3 45

This process continues until the last number in the second column in the file.

I am trying with

awk '{print $2}'

but it is only returning the second column not other contents.

awk '{ print > $2 ".tmp" }' INPUTFILE
1 Like