Something like this:
$ awk '{print > $1}' input
$ cat 1
1 2 3
1 4 5
$ cat a
a z 3
a 3 4
a f g
$ cat b
b b g
Question
I have a file like this -
1 2 3
1 4 5
a z 3
a 3 4
a f g
b b g
I want to split it into multiple files (as many as there are groups) each containing those lines that have the same first field.
1 2 3
1 4 5
a z 3
a 3 4
a f g
b b g
How do I do that? I tried uniq --all-repeated=separate -w 32
but it considers complete line and not just first column when finding duplicates.
Solution
Something like this:
$ awk '{print > $1}' input
$ cat 1
1 2 3
1 4 5
$ cat a
a z 3
a 3 4
a f g
$ cat b
b b g
OTHER TIPS
Slightly nicer file naming approach:
$ ls
file
$ awk '!($1 in a){a[$1]="file"++i}{print > a[$1]}' file
$ ls
file file1 file2 file3
$ cat file1
1 2 3
1 4 5
$ cat file2
a z 3
a 3 4
a f g
$ cat file3
b b g