Score:-1

SED- replace result at file with anther one

cn flag

I am trying to replace result at file with result of anther. Basically trying to replace ip address at netplan file,It does not work.

this is the sentence:

sed -i "s/$(cat /etc/netplan/00-installer-config.yaml |grep - |head -n1 |tr -d "-" |awk -F / '{print $1}')/$(ip add show |grep ens160 |grep inet |awk '{print $2}' |awk -F / '{print $1}')/g" /etc/netplan/00-installer-config.yaml

enter image description here

Thanks in advance,

update:

found the problem needed to delete space sed "s/$(cat /etc/netplan/00-installer-config.yaml |grep - |head -n1 |tr -d "-" |awk -F / '{print $1}' |tr -d " " )/$(ip add show |grep ens160 |grep inet |awk '{print $2}' |awk -F / '{print $1}')/g" /etc/netplan/00-installer-config.yaml

Shay

David avatar
cn flag
What version of Ubuntu are you using? I can see your command but I do not see a result or an error message.
pLumo avatar
in flag
It would help if you separate your commands and save as variables. Then you can check if these are fine and give us information on their output.
pLumo avatar
in flag
please add output of: `echo $(cat /etc/netplan/00-installer-config.yaml |grep - |head -n1 |tr -d "-" |awk -F / '{print $1}')` and `echo $(ip add show |grep ens160 |grep inet |awk '{print $2}' |awk -F / '{print $1}')`
pLumo avatar
in flag
btw, there is `yq` to parse `yaml`.
HELLBOY avatar
cn flag
Hi all, ubuntu 22.04, I am not getting any error but the comand dont replace the ip, I added the picture
pLumo avatar
in flag
Please don't add pictures of text, rather copy paste the text and format as code please. Thanks :-)
HELLBOY avatar
cn flag
I add the results of the commands by your request :)
pLumo avatar
in flag
Btw, easier to `grep` output from `ip` with `-o` flag: `ip -o add show | awk '$2=="ens160" && $3=="inet" {sub("/.*","",$4); print $4}'` ```
terdon avatar
cn flag
Please [edit] your question and provide (as text, not images) your input, and your desired output. We need to see `/etc/netplan/00-installer-config.yaml` so we can understand what `cat /etc/netplan/00-installer-config.yaml | grep - |head -n1 |tr -d "-" |awk -F / '{print $1}'` produces. Most likely it is the internal quotes that are breaking it, but we can't tell if you don't show us the input data.
HELLBOY avatar
cn flag
I am adding text
HELLBOY avatar
cn flag
Never mind found the problem needed to delete the space
Score:0
cn flag

This will be a whole lot more readable if you don't jam everything into one line:

from=$(cat /etc/netplan/00-installer-config.yaml |grep - |head -n1 |tr -d "-" |awk -F / '{print $1}' |tr -d " " )
to=$(ip add show |grep ens160 |grep inet |awk '{print $2}' |awk -F / '{print $1}')

sed "s/$from/$to/g" /etc/netplan/00-installer-config.yaml

And awk itself can do what grep/head/tr can do:

from=$(
  awk '
    /-/ {                     # grep -
      gsub(/-/, "", $0)       # tr -d "-"
      split($0, a, "/")       # awk -F/ ...
      gsub(/ /, "", a[1])     # tr -d " "
      print a[1]
      exit                    # head -n1
    }
  ' /etc/netplan/00-installer-config.yaml
)

to=$(ip add show | awk '/ens160/ && /inet/ {split($2, a, "/"); print a[1]; exit}')

sed "s/$from/$to/g" /etc/netplan/00-installer-config.yaml
Score:0
cn flag

sed "s/$(cat /etc/netplan/00-installer-config.yaml |grep - |head -n1 |tr -d "-" |awk -F / '{print $1}' |tr -d " " )/$(ip add show |grep ens160 |grep inet |awk '{print $2}' |awk -F / '{print $1}')/g" /etc/netplan/00-installer-config.yaml

Will avatar
id flag
Perhaps add a bit of explanatory text?
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.