Sed substitution and external command

Michel Hua

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 ?

Kent

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using a command inside a sed substitution

From Dev

Using a command inside a sed substitution

From Dev

sed substitution command behaving weird

From Dev

Store output of sed with command substitution in it into a variable

From Dev

Bash string substitution for sed argument: Sed: unknown command: `''

From Dev

Bash: Pass Command Substitution to External Program (or Function) without Word Splitting

From Dev

bash command substitution on external script function receives incorrect exit status

From Dev

$! not being set to the PID of a > >(...) process substitution used with an external command

From Dev

Converting vim substitution with back references to sed, awk or similar unix command

From Dev

Why is an extra space getting removed in sed substitution command

From Dev

Can I achieve this substitution with sed? (what does the unknown command: `+' mean)

From Dev

Command substitution with string substitution

From Dev

Command substitution using grep'ed and sed'ed tail -f output as argument source into 'at' command

From Dev

Use $HOSTNAME in sed substitution

From Dev

environment variables substitution with sed

From Dev

sed -i with process substitution

From Dev

Expect, sed, and Variable Substitution

From Dev

sed substitution including newlines

From Dev

Sed - string substitution with groupings

From Dev

Grouping and substitution with sed

From Dev

string substitution using sed

From Dev

How to perform this substitution in sed?

From Dev

sed substitution on multiple lines

From Dev

sed: reversing pattern substitution

From Dev

sed conditional substitution

From Dev

Sed Command substitution how do you treat spaces or multiple spaces as one?

From Dev

Why does sed substitution command with the flag p print the modified output twice?

From Dev

accidentally ran sed command, does unknown option to `s' mean no substitution happened?

From Dev

Pipelined Sed does not work on found filename inside Bash command substitution when invoked from Find "-exec"

Related Related

  1. 1

    Using a command inside a sed substitution

  2. 2

    Using a command inside a sed substitution

  3. 3

    sed substitution command behaving weird

  4. 4

    Store output of sed with command substitution in it into a variable

  5. 5

    Bash string substitution for sed argument: Sed: unknown command: `''

  6. 6

    Bash: Pass Command Substitution to External Program (or Function) without Word Splitting

  7. 7

    bash command substitution on external script function receives incorrect exit status

  8. 8

    $! not being set to the PID of a > >(...) process substitution used with an external command

  9. 9

    Converting vim substitution with back references to sed, awk or similar unix command

  10. 10

    Why is an extra space getting removed in sed substitution command

  11. 11

    Can I achieve this substitution with sed? (what does the unknown command: `+' mean)

  12. 12

    Command substitution with string substitution

  13. 13

    Command substitution using grep'ed and sed'ed tail -f output as argument source into 'at' command

  14. 14

    Use $HOSTNAME in sed substitution

  15. 15

    environment variables substitution with sed

  16. 16

    sed -i with process substitution

  17. 17

    Expect, sed, and Variable Substitution

  18. 18

    sed substitution including newlines

  19. 19

    Sed - string substitution with groupings

  20. 20

    Grouping and substitution with sed

  21. 21

    string substitution using sed

  22. 22

    How to perform this substitution in sed?

  23. 23

    sed substitution on multiple lines

  24. 24

    sed: reversing pattern substitution

  25. 25

    sed conditional substitution

  26. 26

    Sed Command substitution how do you treat spaces or multiple spaces as one?

  27. 27

    Why does sed substitution command with the flag p print the modified output twice?

  28. 28

    accidentally ran sed command, does unknown option to `s' mean no substitution happened?

  29. 29

    Pipelined Sed does not work on found filename inside Bash command substitution when invoked from Find "-exec"

HotTag

Archive