假设所有的线包含日期:
$ cat file
Fri Nov 6 07:52:02
...
$ awk 'BEGIN {
months["Jan"] = 1;
months["Feb"] = 2;
months["Mar"] = 3;
months["Apr"] = 4;
months["May"] = 5;
months["Jun"] = 6;
months["Jul"] = 7;
months["Aug"] = 8;
months["Sep"] = 9;
months["Oct"] = 10;
months["Nov"] = 11;
months["Dec"] = 12;
}
{
month = months[$2];
printf("%s-%02d-%02d %s\n", 2015, month, $3, $4);
}' file > out
$ cat out
2015-11-06 07:52:02
...
如果外核层你需要修改一些你可以稍微调整一下awk脚本的行,例如。匹配包含2015
每一行:
...
# Match every line containing 2015
/2015/ {
month = months[$2];
printf("%s-%02d-%02d %s\n", 2015, month, $3, $4);
# Use next to prevent this the other print to happen for these lines
# Like 'continue' in while iterations
next;
};
# This '1' will print all other lines as well:
# Same as writing { print $0 }
1
您可以发布样本数据的两行呢?另外,你的'grep'在其中有2015年,但你的日期没有。 – houtanb
2015年的grep只是因为这是线上唯一的对象,对其他任何东西都可能得到误报。 周五11月6日9点09分18秒 周五11月6日9时14分46秒 是从猫/ grep的输出的2行。还是你想要没有吝啬? – user1972018
我只是说在你的文本文件中有这样一个日期:'Fri Nov 6 07:52:02'没有2015,所以对2015年的吝啬不会给你任何东西。我猜这是一个复制粘贴错误。如果您使用示例行更新您的问题,我可以在下面更新我的答案以使用它。 – houtanb