3

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?

Community
  • 1
  • 1
Lenwood
  • 1,371
  • 16
  • 36
  • When you way "no response"; how long are you waiting? – William Pursell Mar 15 '13 at 19:33
  • It runs for less than a second (the prompt returns). I've watched the folder for a few minutes to see if anything populates, but nothing. I've also searched my system to see if the files are being created elsewhere, but no luck. – Lenwood Mar 15 '13 at 19:39
  • Resolved. It was my line endings. Following the leadings of [this thread](http://stackoverflow.com/a/2652526/297780), I used the `file data.csv` command to check the file format. I had Mac style line endings, so I used Text Wrangler to change the formatting and now the code above works as expected. – Lenwood Mar 15 '13 at 19:51
  • @Lenwood - add that as an answer and accept so that this question is closed. No points for you though :-) – Fredrik Pihl Mar 15 '13 at 20:00
  • @FredrikPihl I've added the answer below. Can I mark it as closed now, or do I have to wait 2 days? – Lenwood Mar 15 '13 at 20:11
  • don't know actually. Try and see if it works – Fredrik Pihl Mar 15 '13 at 20:12

2 Answers2

5

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.

Community
  • 1
  • 1
Lenwood
  • 1,371
  • 16
  • 36
  • The posted command will produce output but it's probably overly long and inefficient. The script is closing the input file after every line and then reopening it on the next matching line. It's probably doing that to have as few output files as possible open simultaneously but with modern awks like gawk that's simply not an issue. You should be able to just do: `awk -F, 'NR>1 {print > ($1 ".csv")}' file.csv` – Ed Morton Mar 16 '13 at 02:52
0

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
alfiogang
  • 435
  • 5
  • 7