This has been asked many times before but I simply can't implement the solutions properly. I have a large csv named 2017-01.csv, with a date column (it's the second column in the file) and I am splitting the file by date. The original file looks like:
date
2017-01-01
2017-01-01
2017-01-01
2017-01-02
2017-01-02
2017-01-02
After the split, 2017-01-01.csv looks like
2017-01-01
2017-01-01
2017-01-01
and 2017-01-02.csv looks like
2017-01-02
2017-01-02
2017-01-02
The code I am using is
awk -F ',' '{print > (""$2".csv")}' 2017.csv
Everything works fine but I need to keep the header row. So I tried
awk -F ',' 'NR==1; NR > 1{print > (""$2".csv")}' 2017-01.csv
But I still get the same results without the header row. What am I doing wrong? I read answers to many similar questions on Stackoverflow but I just can't understand what they are doing.
I want this:
2017-01-01.csv should look like
date
2017-01-01
2017-01-01
2017-01-01
2017-01-02.csv should look like
date
2017-01-02
2017-01-02
2017-01-02
""in your script is doing nothing, you could just remove it. edit your question to provide sample input/output that more truly represents your real multi-columnar data so we can help you.