Awk 仅从日期和时间中提取日期

Awk 仅从日期和时间中提取日期,awk,Awk,以下awk代码按预期工作 2012-03-10~12:59:41等日期四舍五入为最接近的五分钟,例如2012-03-10~12:55:00 我如何更改它,以便获得像20120310这样的年-月-日(不带破折号) 将拆分行更改为使用gensub(): 输出: set 1:2:4 20120318~22:05:00\r\n 拆分($7,a,“-”)正在工作,但第03个月被转换为3个月,因为我使用了[2]=int(a[2]);如何转换为文本?这个a[2]=var(a[2]);不起作用。正确。谢谢你的

以下awk代码按预期工作

2012-03-10~12:59:41等日期四舍五入为最接近的五分钟,例如2012-03-10~12:55:00

我如何更改它,以便获得像20120310这样的年-月-日(不带破折号)


将拆分行更改为使用
gensub()

输出:

set 1:2:4 20120318~22:05:00\r\n

拆分($7,a,“-”)正在工作,但第03个月被转换为3个月,因为我使用了[2]=int(a[2]);如何转换为文本?这个a[2]=var(a[2]);不起作用。正确。谢谢你的快速回答。我只需要日期部分。20120318足够了~ 22:05:00不需要。
$ cat file 
1^2^3^4^5^6^2012-03-18~22:09:10

awk 'BEGIN {
         # all fields are separated by ^
         FS = "^";
     }
     {
         # $7 is the date and time in the form yyyy-mm-dd hh:mm:ss.
         # Split at colons to get hours minutes and seconds into a[1]
         # through a[3].  Round minutes to nearest 5.
         split(gensub(/-/,"","g",$7),a,":")
         a[2] = int(a[2] / 5) * 5;
         # print first, second and fourth field, then rounded time.
         printf "set %s:%s:%s %s:%02d:00\\r\\n\n", $1, $2, $4, a[1], a[2];
     }' file
set 1:2:4 20120318~22:05:00\r\n