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
...
I would want to output it in this format
2012-05-23
a;b;c
2012-05-21
d;e;f
2012-05-19
g;h;i
...
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
...
Using date
, I know how to convert dates :
% date -d 'May 23 2012 11:59:56' '+%Y-%m-%d'
2012-05-23
But how can make match_string to be evaluated during the sed command ?
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments