简体   繁体   中英

Running "ip | grep | awk" within a sed replacement

Problem Set (Raspberry Pi OS): I have a file " example.conf " that contains a line IPv4addr=XXXXX . I am attempting to change this to the IP that is generated the in the command ipTest=$(ip --brief a show | grep eth0 | awk '{ print $3 }')

I want to automate this file change during a script "install.sh", the line I am attempting is:

IPtest=$(ip --brief a show | grep eth0 | awk '{ print $3 }')

sudo sed -e "/IPv4addr/s/[^=]*$/$IPtest/" example.conf

Returns error: sed: -e expression #1, char 32: unknown option to `s'

A simple line in that code works, such as SimpleTest='Works'

Any thoughts? I am open to other solutions as well, however I am not an experienced linux user so I am using the tools I know to work with other problem sets.

You can shorten your variable and allow awk to do the job of grep at the same time

IPtest=$(ip --brief a s | awk '/eth0/{print $3}')

Using sed grouping and back referencing

sed -i.bak "s|\([^=]*.\).*|\1$IPtest|" example.conf

$IPtest contains "/" character, try something like that

IPtest=$(ip --brief a show | grep eth0 | awk '{ print $3 }')

sudo sed -e '/IPv4addr/s@[^=]*$@'"$IPtest"'@' example.conf

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