Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using awk to split CSV file by column

Tags:

bash

macos

csv

awk

I have a CSV file that I need to split by date. I've tried using the AWK code listed below (found elsewhere).

awk -F"," 'NR>1 {print $0 >> ($1 ".csv"); close($1 ".csv")}' file.csv

I've tried running this within terminal in both OS X and Debian. In both cases there's no error message (so the code seems to run properly), but there's also no output. No output files, and no response at the command line.

My input file has ~6k rows of data that looks like this:

date,source,count,cost
2013-01-01,by,36,0
2013-01-01,by,42,1.37
2013-01-02,by,7,0.12
2013-01-03,by,11,4.62

What I'd like is for a new CSV file to be created containing all of the rows for a particular date. What am I overlooking?

like image 768
Lenwood Avatar asked Jun 29 '26 04:06

Lenwood


1 Answers

I've resolved this. Following the logic of this thread, I checked my line endings with the file command and learned that the file had the old-style Mac line terminators. I opened my input CSV file with Text Wrangler and saved it again with Unix style line endings. Once I did that, the awk command listed above worked as expected. It took ~5 seconds to create 63 new CSV files broken out by date.

like image 136
Lenwood Avatar answered Jul 01 '26 18:07

Lenwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!