sed替换和外部命令 [英] Sed substitution and external command
本文介绍了sed替换和外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这种格式的输入日志文件
I have an input log file in this format
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
,我知道如何替换日期行
Using sed
, I know how to substitute the date lines
% sed 's/.*:.*:.*/match_string/' input.txt
match_string
a;b;c
match_string
d;e;f
match_string
g;h;i
...
使用 date
,我知道如何转换日期:
Using date
, I know how to convert dates :
% date -d 'May 23 2012 11:59:56' '+%Y-%m-%d'
2012-05-23
但是在sed命令期间如何使match_string求值?
But how can make match_string to be evaluated during the sed command ?
推荐答案
如果有 GNU Sed 可用,则可以使用 e
标志:
If you have GNU Sed available, you could use the e
flag:
sed 's/.*:.*/date -d"&" "+%Y-%m-%d"/ge' file
将为您的示例提供帮助,请参见测试:
will help you for your example, see the test:
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
这篇关于sed替换和外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文