This is a combination of two other questions (how to split a file by each line prefix and how to split a file according to a column, including the header). I want to go from this content in input.csv:
id,first,second,third
1,a,b,c
333,b,b,b
1,d,e,f
2,d,e,f
1,c,d,e
333,a,a,a
[more lines in the same format]
to this content in 1.csv:
id,first,second,third
1,a,b,c
1,d,e,f
1,c,d,e
, this content in 2.csv:
id,first,second,third
2,d,e,f
, and this content in 333.csv:
id,first,second,third
333,b,b,b
333,a,a,a
, that is:
- Put all the lines with ID of N into
N.csv. - Keep the sequence of lines as in the original.
- Include the header from the original file in all the output files.
This must also be really fast, so a while read loop is not going to cut it.
awkis fine if you're working with simple csv files (i.e. there are no delimiters embedded in the fields); if you're dealing with more complex csv files my advice is to use a proper tool (e.g. perl, python etc)