Reputation: 81
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
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
-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