bash – Sed替换和外部命令

我有这种格式的输入日志文件
May 23 2012 11:59:56
a;b;c
May 21 2012 16:54:12
d;e;f
May 19 2012 16:22:52
g;h;i
...

我想以这种格式输出它

2012-05-23
a;b;c
2012-05-21
d;e;f
2012-05-19
g;h;i
...

使用sed,我知道如何替换日期行

% sed 's/.*:.*:.*/match_string/' input.txt
match_string
a;b;c
match_string
d;e;f
match_string
g;h;i
...

使用日期,我知道如何转换日期:

% date -d 'May 23 2012 11:59:56' '+%Y-%m-%d'
2012-05-23

但是如何在sed命令期间对match_string进行求值?

如果你有GNU Sed可用,你可以使用e标志:
sed 's/.*:.*/date -d"&" "+%Y-%m-%d"/ge' file

将帮助您的示例,请参阅测试:

kent$ echo "May 23 2012 11:59:56
a;b;c
May 21 2012 16:54:12
d;e;f
May 19 2012 16:22:52
g;h;i"|sed 's/.*:.*/date -d"&" "+%Y-%m-%d"/ge'
2012-05-23
a;b;c
2012-05-21
d;e;f
2012-05-19
g;h;i

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...