Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sed or perl replacing sub string only, with the result of other command

How, by one-liner sed or perl, to replace a sub string only, with the result of other command execution, in the middle of full line string e.g:

$ cat f
iiiiiii
OOO=dd=XXX
uuuuuuu

it’s clear in string line with distinct substring patterned =dd= would be captured to be executed by a command, say, type -p

e.g. sed is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$ sed -E 's/^O+=([a-z]\w+)=X+$/type -p \1/e' f

but the result is only the command execution’s one, with no complete two adjacent strings, i.e. fails to get the correct solution

OOO=/usr/bin/dd=XXX

So help out how to accomplish up to the correct solution by sed or perl (or both if being so generous)

>Solution :

You need to use more capture groups and tweaking of replacement string to get it right like this:

sed -E 's/^(O+=)([a-z]\w*)(=X+)$/echo "\1$(type -p \2)\3"/e' f

iiiiiii
OOO=/usr/bin/dd=XXX
uuuuuuu

We have 3 capture groups:

  • ^: Start
  • (O+=): Match 1+ of O followed by a =. Capture this in 1st group
  • ([a-z]\w*): Match [a-z] followed by 0 or more word characters. Capture this in 2nd group
  • (=X+): Match = followed by 1+ of X. Capture this in 3rd group
  • $: End
  • echo "\1$(type -p \2)\3 is used to put \1 followed by type -p \2 followed by \3 in the substitution.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading