dr. gruselglatz
dr. gruselglatz

Reputation: 81

how to take a date in csv and convert it from european timestamp to the american format in bash?

I want to convert the date in the first row of my csv to have the american notation. The best way for me would be to go with the bash. But date doesn't linke my format and my attempt with awk was horrible.

Original CSV:

Datum;Verbrauch
15.05.2016 00:00;0,075
15.05.2016 00:15;0,075
15.05.2016 00:30;0,075
15.05.2016 00:45;0,075
15.05.2016 01:00;0,075

Wanted CSV:

Datum;Verbrauch
2016-05-15 00:00:00;0,075
2016-05-15 00:15:00;0,075
2016-05-15 00:30:00;0,075
2016-05-15 00:45:00;0,075
2016-05-15 01:00:00;0,075

thx for your help

Upvotes: 3

Views: 88

Answers (1)

John1024
John1024

Reputation: 113834

The power of date is not needed here: the format you need can be achieved just using printf:

$ awk -F'[ .:;]' 'NR==1{print;next} {printf "%s-%s-%s %s:%s:00;%s\n",$3,$2,$1,$4,$5,$6}' file
Datum;Verbrauch
2016-05-15 00:00:00;0,075
2016-05-15 00:15:00;0,075
2016-05-15 00:30:00;0,075
2016-05-15 00:45:00;0,075
2016-05-15 01:00:00;0,075

How it works

  • -F'[ .:;]'

    Tells awk to use any of a space, a period, a colon, or a semicolon as the field separator

  • NR==1{print;next}

    This prints the header line unchanged

  • printf "%s-%s-%s %s:%s:00;%s\n",$3,$2,$1,$4,$5,$6

    This reformats the line as you want.

Upvotes: 4

Related Questions