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?

6
  • When you way "no response"; how long are you waiting? Commented Mar 15, 2013 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. Commented Mar 15, 2013 at 19:39
  • Resolved. It was my line endings. Following the leadings of this thread, 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. Commented Mar 15, 2013 at 19:51
  • @Lenwood - add that as an answer and accept so that this question is closed. No points for you though :-) Commented Mar 15, 2013 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? Commented Mar 15, 2013 at 20:11

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.