3

I have some data in a file in the form of csv of the form:

ID,DATE,EARNING
1,12 May 2018,5
1,13 May 2018,15
2,12 May 2018,25

I want to split this into multiple files such that file_1_May_report contains:

ID,DATE,EARNING
1,12 May 2018,5
1,13 May 2018,15

and another file file_2_May_report that contains:

ID,DATE,EARNING
2,12 May 2018,25

I have tried :

awk -F, '{print >> $1}' input.csv 

However I only get one file 1 with only one record, that is the last record in the input file. How do I get it to split into multiple files based on ID?

0

1 Answer 1

1

You may use this awk:

awk -F, 'NR==1{hdr=$0; next} !seen[$1]++{fn="file_" $1 "_May_report"; print hdr > fn} {print > fn}' input.csv

Or with a more readable format:

awk -F, 'NR == 1 {
   hdr = $0
   next
}
!seen[$1]++ {
   fn = "file_" $1 "_May_report"
   print hdr > fn
}
{
   print > fn
}' input.csv
Sign up to request clarification or add additional context in comments.

3 Comments

I had properly tested this awk before posting. Can you clarify what didn't work?
It doesnt create any files
This worked. The problem was my file has the wrong line terminators. had to run tr '^M' '\n' <input.csv > unix-input.csv

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.