Home > Enterprise >  How to run a command in sed in Linux?
How to run a command in sed in Linux?

Time:12-22

sed -i 's/1.1.1.1/ `hostname -I | cut -f1 -d " "`/g' file.txt

Not able to overwrite IP address using sed command in a given file. How to run this (hostname -I | cut -f1 -d " ") command with sed command?

CodePudding user response:

This might work for you (GNU sed):

sed -i '/1\.1\.1\.1/{s//$(hostname -I | cut -f1 -d " ")/;s/.*/echo "&"/e}' file

Place the commands between $(...) and then echo the line using the evaluate flag of the substitution command.

CodePudding user response:

Just use a variable.

hn=$(hostname -I | cut -f1 -d" ") && sed -i "s/1\.1\.1\.1/$hn/g" file

Sed's e and s///e work on the entire pattern space, so it's just more trouble than it's worth.

  • Related