Changing date format in CSV file

I have a CSV file with a date format like this;

11/19/2012 17:37:00,1.372,121.6
11/19/2012 17:38:00,0.743,121.6

Want to change the time stamp to seconds after 1970 so I can get the data in rrdtool. For anyone interested, this is data from a TED5000 unit and is Kwatts and volts.

Needs to look like;

1353364620,1.372,121.6
1353364680,0.743,121.6

I know that the date command will convert the time;

date +"%s" -d '11/19/2012 17:38:00'

And that the sed command will search and replace. Just trying the best way to get the data in the correct format and then use the update command in rrdtool to get the data in the rrd data base.

Try:

while IFS="," read d a b; do d=`date +"%s" -d "$d"`; echo "$d,$a,$b"; done < input > output
1 Like

Thanks, works great, just what I needed.

If your CSV file is large, you might be considering this awk alternative:

 awk -F, '{"date +%s -d\""$1 "\""| getline dte;$1=dte}1' OFS="," file

This should be *much* faster on large files. About 350 times faster on a 3000 lines sample file.