Pregunta

I am trying to format some date data in a CSV file.

~/temperature$ cat m
1 01/04/13 02:20:07 PM 21.843 24.360 981.5   
2 01/04/13 02:25:07 PM 21.509 27.048 335.1   
3 01/04/13 02:30:07 PM 19.555 31.441 335.1   
4 01/04/13 02:35:07 PM 18.628 32.154 335.1   
5 01/04/13 02:40:07 PM 18.152 31.782 327.2   
6 01/04/13 02:45:07 PM 17.962 34.723 327.2   
7 01/04/13 02:50:07 PM 17.867 33.008 335.1   
8 01/04/13 02:55:07 PM 17.819 35.722 327.2   
9 01/04/13 03:00:07 PM 17.819 33.989 327.2   
10 01/04/13 03:05:07 PM 17.796 36.143 327.2

I would like to convert the date to "YYYY-MM-DD HH:MM:SS" format.

Here is what I tried:

~/temperature$ awk '{("date \"+%Y-%m-%d %T\" --date \"$2 $3\"")|getline t; print t}' m
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00
2014-03-28 00:00:00

~/temperature$ awk '{("date \"+%Y-%m-%d %T\" -d "$2 )|getline t; print t}' m
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
2013-01-04 00:00:00
~/temperature$ awk '{("date \"+%Y-%m-%d %T\" -d "$2" "$3)|getline t; print t}' m
date: extra operand `02:20:07'
Try `date --help' for more information.

date: extra operand `02:25:07'
Try `date --help' for more information.

...
...

So both the approaches give the wrong date. Any ideas on how to fix it? Many thanks.

DY.

¿Fue útil?

Solución

This would work:

$ awk '
{
    get_date = "date \"+%Y-%m-%d %T\" -d \""$2" "$3" "$4"\""
    get_date | getline new_date
    print new_date
}' file
2013-01-04 14:20:07
2013-01-04 14:25:07
2013-01-04 14:30:07
2013-01-04 14:35:07
2013-01-04 14:40:07
2013-01-04 14:45:07
2013-01-04 14:50:07
2013-01-04 14:55:07
2013-01-04 15:00:07
2013-01-04 15:05:07

Otros consejos

Is this all you're trying to do?

$ awk '{split($2,d,"/"); $2="20"d[3]"-"d[1]"-"d[2]}1' file
1 2013-01-04 02:20:07 PM 21.843 24.360 981.5
2 2013-01-04 02:25:07 PM 21.509 27.048 335.1
3 2013-01-04 02:30:07 PM 19.555 31.441 335.1
4 2013-01-04 02:35:07 PM 18.628 32.154 335.1
5 2013-01-04 02:40:07 PM 18.152 31.782 327.2
6 2013-01-04 02:45:07 PM 17.962 34.723 327.2
7 2013-01-04 02:50:07 PM 17.867 33.008 335.1
8 2013-01-04 02:55:07 PM 17.819 35.722 327.2
9 2013-01-04 03:00:07 PM 17.819 33.989 327.2
10 2013-01-04 03:05:07 PM 17.796 36.143 327.2

or do you need to change the time format too?

awk '{split($2,d,"/"); $2="20"d[3]"-"d[1]"-"d[2];
      split($3,t,":"); if(sub(/PM /,"")) t[1]+=12; $3=t[1]":"t[2]":"t[3]
     }1' file
1 2013-01-04 14:20:07 21.843 24.360 981.5
2 2013-01-04 14:25:07 21.509 27.048 335.1
3 2013-01-04 14:30:07 19.555 31.441 335.1
4 2013-01-04 14:35:07 18.628 32.154 335.1
5 2013-01-04 14:40:07 18.152 31.782 327.2
6 2013-01-04 14:45:07 17.962 34.723 327.2
7 2013-01-04 14:50:07 17.867 33.008 335.1
8 2013-01-04 14:55:07 17.819 35.722 327.2
9 2013-01-04 15:00:07 17.819 33.989 327.2
10 2013-01-04 15:05:07 17.796 36.143 327.2
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top