I am attempting to split a csv file based on unique column values into multiple files using awk. I am able to split the csv successfully with awk -F\, '{print > $2".csv"}' example.csv however it is committing the header column from the new files.
For example:
example.csv
Color,Car,Make
Red,Kia,Spectra
Orange,Kia,Sportage
Green,Ford,Explorer
Black,Ford,F-150
Result:
Kia.csv
Red,Kia,Spectra
Orange,Kia,Sportage
___________________
Ford.csv
Green,Ford,Explorer
Black,Ford,F-150
My desired output:
Kia.csv
Color,Car,Make
Red,Kia,Spectra
Orange,Kia,Sportage
___________________
Ford.csv
Color,Car,Make
Green,Ford,Explorer
Black,Ford,F-150
To attempt getting the header column passed to the new files, I attempted something like this awk -F'|' 'FNR==1{hdr=$0;next} {if (!seen[$1]++) print hdr>$2; print>$2}' example.csv but unfortunately this did not have the intended result.