简体   繁体   中英

How to run a command in sed in Linux?

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM