0

My problem is very similar to this. I basically want to add a column of timestamps to a .csv-file with bash. I'm using this script:

cat foo.csv | while read line ; do echo $line\;$(date -d "$t" "+%s") ; done

and this .csv-file (foo.csv):

2011-11-25  12:00:00
2010-11-25  13:00:00
2009-11-25  14:00:00

but instead of getting the expected output, I get an output like this:

2011-11-25,12:00:00;1400882400
2010-11-25,13:00:00;1400882400
2009-11-25,14:00:00;1400882400

Anybody who knows what might be wrong?

Community
  • 1
  • 1
ljnissen
  • 139
  • 1
  • 12

1 Answers1

0

If you need parsing the date from incoming file and printing it timestamp use this:

date --date="$line" "+%s"
Jan Drábek
  • 146
  • 6
  • This I know, but it doesn't really help me with the strange output from the .csv-file. – ljnissen May 24 '14 at 20:32
  • If you problem is wrong formatting of the first line than you have to format it too as suggested in shellter comment. date --date="2011-11-25 12:00:00" "+%Y/%m/%d %H:%M:%S; %s" And you probable want to leave the echo out. If your problem are the wrong timestamp its because you are not providing correct date to the function ($t instead of $line in your command). – Jan Drábek May 25 '14 at 06:06
  • 1
    Got it. The magic happened when I combined your knowledge with shellter's and f4m8's from the other thread. This code does the job: cat foo.csv | while read line ; do echo $line\,$(date -d "${line//,/ }" "+%s") ; done Thank you! – ljnissen May 25 '14 at 10:35