Lenwood
Lenwood

Reputation: 1404

Using awk to split CSV file by column

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?

Upvotes: 3

Views: 6921

Answers (2)

alfiogang
alfiogang

Reputation: 494

For retrieve information in a log file with ";" separator I use:

grep "END SESSION" filename.log | cut -d";" -f2

where

  -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
  -f, --fields=LIST       select only these fields;  also print any line
                          that contains no delimiter character, unless
                          the -s option is specified

Upvotes: 0

Lenwood
Lenwood

Reputation: 1404

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.

Upvotes: 5

Related Questions