Amit Solanki
Amit Solanki

Reputation: 65

Split file by date column value

I have a csv file that has 89 columns and it contains multiple dates of data. The date column is the 59th column. The date format is mm/dd/yy. What I would like to achieve is to create a file with mmddyyyy.csv, so each file will contain only one date of data.

So far my approach is

  1. Get all distinct date from files
  2. For each distinct date

    grep [date value] file > mmddyyyy.csv
    

The only drawback of the above solution is that if [date value] exists anywhere in the line, it will be picked up and I possibly will end up with one record in two or more files.

I know for a single value (string/number) awk can do it straight forward like

awk -F"\t" '{print >> ($14".csv");}' $1

Upvotes: 0

Views: 1134

Answers (1)

Ed Morton
Ed Morton

Reputation: 203393

It sounds like all you need is:

awk -F, '{split($59,t,"/"); print > (t[1] t[2] "20" t[3] ".csv")}' file

but without sample input and expected output it's a guess. If you're not using GNU awk you might need to throw in a close() when appropriate to avoid having too many files open simultaneously.

Upvotes: 2

Related Questions