我正在寻找awk命令将文本文件的时间列更改为最近的5分钟间隔。所以12:56:59成为12:55:00
以下命令按预期工作。谢谢你的回复。
head r_SERVER_2012-03-10-12-55-00 | awk -F'^' '{print $7}' | awk '{split($2, a, ":"); printf "%s %s:%02d:00\n", $1, a[1],int(a[2]/5)*5}'
正确的结果:
2012-03-10 12:55:00
但我想要显示除日期以外的字段。以下不起作用:
head r_SERVER_2012-03-10-12-55-00 | awk -F'^' '{print $1, $2, $7, $8}' | awk '{split($2, a, ":"); printf "%s %s:%02d:00\n", $1, a[1],int(a[2]/5)*5}'
错误的结果:
565 14718:00:00
应该是......
565 123 2012-03-10 12:55:00 country
答案 0 :(得分:2)
您不需要多个awk命令,只需一个,您可以在脚本中设置awk的FS
(字段分隔符)变量,您可以将其放入文件中:
$ cat foo.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($7, a, ":");
a[2] = int(a[2] / 5) * 5;
# print first and second field, then rounded time, then 8th field.
printf "%s %s %s:%02d:00 %s\n", $1, $2, a[1], a[2], $8;
}
$ cat input
565^123^$3^$4^$5^$6^2012-03-10 12:56:59^country
$ awk -f foo.awk < input
565 123 2012-03-10 12:55:00 country
$