I have a file that I would like to break up into multiple files with uniq values for the first column. For example, here is a file:
fileA.txt
1 Cat
1 Dog
1 Frog
2 Boy
2 Girl
3 Tree
3 Leaf
3 Branch
3 Trunk
I would like my output to look something like this:
file1.txt
1 Cat
2 Boy
3 Tree
file2.txt
1 Dog
2 Girl
3 Leaf
file3.txt
1 Frog
3 Branch
file4.txt
3 Trunk
If a value does not exist, I want it to be skipped. I have tried to search for similar situations to mine, but I've come up short. Does anyone have idea of how to do this?
Theoretically, this awk command should work: awk '{print > "file" ++a[$1] ".txt"}' input. However, I can't get it to work appropriately (most likely due to the fact that I work on a mac) Does anyone know of an alternative way?