Input file: i have a file and need to split into multiple files based on first column. i need the header for all the splitted files. I'm unable to get the header.
$ cat log.txt
id,mailtype,value
1252468812,yahoo,3.5
1252468812,hotmail,2.4
1252468819,yahoo,1.2
1252468812,msn,8.9
1252468923,gmail,12
1252468819,live,3.4
1252468929,yahoo,9.0
1252468929,msn,1.2
Required:
Split the above files based on the first field (i.e. lines with same first field should go to the same file)
The awk one liner:
$ awk -F "," '{close(f);f=$1}{print > f".txt"}' log.txt
Output:
Above file is splited into the following sub-files.
$ cat 1252468812.txt
1252468812,yahoo,3.5
1252468812,hotmail,2.4
1252468812,msn,8.9
$ cat 1252468819.txt
1252468819,yahoo,1.2
1252468819,live,3.4
$ cat 1252468923.txt
1252468923,gmail,12
$ cat 1252468929.txt
1252468929,yahoo,9.0
1252468929,msn,1.2