我正在尝试在CSV文件中格式化一些日期数据。
~/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
我想将日期转换为" YYYY-MM-DD HH:MM:SS"格式。
以下是我的尝试:
~/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.
...
...
所以这两种方法都给出了错误的日期。关于如何修复它的任何想法? 非常感谢。
DY
答案 0 :(得分:3)
这样可行:
$ 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
答案 1 :(得分:3)
这是你想要的吗?
$ 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
或者您是否也需要更改时间格式?
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