Sed command to replace text

Solved
thierryR51 Posted messages 144 Status Member -  
zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   -
Hello,
I'm trying to replace an IP in the hosts file
cat /etc/hosts
80.119.80.108 Backup


I tried this:
sed -e "s/Backup/80.119.80.109 Backup/g" -i /etc/hosts;


But the result I get is this;
80.119.80.108 80.119.80.109 Backup

I wanted to remove 80.119.80.108 but I'm stuck in the sem....... I'm almost there, but a little help would be appreciated.
Thank you

Configuration: Mageia5 on Toshiba Satellite L350

--
Researcher in improvements.

[url]

4 answers

  1. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
     
    Hello;

    sed -i.bak '/Backup/{s/^[^ ]*/80.119.80.109/}' /etc/host
    s

    --
    _______________________________ ☯ Zen my nuggets ☮ ______________________________
    Do something for the environment, close your windows and adopt a penguin… 🐧
    1
    1. thierryR51 Posted messages 144 Status Member 8
       
      If I embed it in my script with variables
      sed -i.bak '/'$nameServeur'/{s/^[^ ]*/'$newIp'/}' /etc/hosts

      I get an error about the first character.
      sed -i.bak / 'MaMaison/{s/^[^ ]*/86.66.183.203/}' /etc/hosts
      sed: -e expression no. 1, character 1: unfinished regular expression address

      It seems to me that I wrote it correctly....
      0
    2. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501 > thierryR51 Posted messages 144 Status Member
       
      There is an extra space in the name of your variable $nameServeur ;-(

      / 'MaMaison/


      $ name=" Sauvegarde";nip="80.119.80.109"

      $ sed '/'${name}'/{s/^[^ ]*/'${nip}'/}' plop
      sed: -e expression n°1, character 1: incomplete address regular expression

      $ name="Sauvegarde";nip="80.119.80.109"

      $ sed '/'${name}'/{s/^[^ ]*/'${nip}'/}' plop
      80.119.80.109 Sauvegarde
      0
    3. thierryR51 Posted messages 144 Status Member 8
       
      Thank you Zipe31. I can't stop thanking you... It's going to get boring eventually. ;)
      0
    4. thierryR51 Posted messages 144 Status Member 8
       
      However, I did what was necessary to avoid having this blank
      nameServeur=${Serveurname%% }

      but it is still there.
      0
    5. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501 > thierryR51 Posted messages 144 Status Member
       
      What does the starting variable ${Serveurname} look like?
      0
  2. thierryR51 Posted messages 144 Status Member 8
     
    Thank you Zipe31. You're still the great bash specialist...
    Have a good evening.

    --
    Researcher in improvements.

    [url]
    0
    1. thierryR51 Posted messages 144 Status Member 8
       
      1 line of hosts is broken down as follows:
      IP + TAB + hostname

      I think the whole problem is about inserting the TAB.
      It seems that there are several SED versions and not all of them handle '\t'
      http://sed.sourceforge.net/sed1line_fr.html
      0
    2. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501 > thierryR51 Posted messages 144 Status Member
       
      sed '/Backup/{s/[^\t]*/80.119.80.109/}' /etc/hosts
      0
    3. thierryR51 Posted messages 144 Status Member 8
       
      I adapted it to my variables. In fact, it writes nothing. I only have the proofreading of my file.
      sed '/'${nameServeur}'/{s/^[^\t]*/'${newIp}'/}' $filehosts

      or $nameServeur = MaMaison
      $newIp=80.119.80.109
      $filehosts= /etc/hosts

      Is it because the line containing 'MaMaison' no longer exists in my file?

      In fact, I would like:
      If the line exists: Replace it
      If it does not exist: Create it.

      Thank you for your help. The sed command is complicated...
      0
    4. UnGnU Posted messages 1468 Status Contributor 158 > thierryR51 Posted messages 144 Status Member
       
      Hello,

      An example of a hosts file without the entry MaMaison
      $ cat my_hosts_file
      80.119.80.108 Backup

      An example with both entries
      $ cat my_home_file
      80.119.80.108 Backup
      192.168.10.1 MaMaison

      The script file for sed :
      $ cat script.sed
      /Backup/{
      s/[^\t]*/80.119.80.109/
      }

      /MaMaison/{
      s/[^\t]*/192.168.10.2/
      b
      }

      $ {
      a\192.168.10.2\tMaMaison
      }


      The script executed on the file without the occurrence of MaMaison :
      $ sed -f script.sed my_hosts_file
      80.119.80.109 Backup
      192.168.10.2 MaMaison


      The script executed on the file already having both occurrences
      $ sed -f script.sed my_home_file
      80.119.80.109 Backup
      192.168.10.2 MaMaison
      0
    5. thierryR51 Posted messages 144 Status Member 8
       
      OK, I see that with sed the solution exists. So to integrate this into my script, I did this. I don't have a second sed file. I'm integrating everything into my script.
      sed -e '/'${nameServeur}'/{s/^[^\t]*/'${newIp}'/}';$ '{a\'${newIp}'\t'${nameServeur}'}' $filehosts

      The return of the command is
      + sed -e '/MaMaison/{s/^[^\t]*/86.66.183.130/}'


      ^C

      As you can see, it's blocking. I had to terminate the script manually. Apparently, the ";" doesn't seem to be taken into account. Yet I followed the advice from https://openclassrooms.com/courses/la-commande-sed where they clearly state that for a single-line command, we separate with ";"

      Did I misunderstand?
      0
  3. thierryR51 Posted messages 144 Status Member 8
     
    Little problem. When sed wants to write

     sed -i -e '/MyHouse/{s/^[^\t]*/86.66.183.119/}' -e b -e '$ a\86.66.183.119\tMyHouse' /etc/hosts

    and the line does not exist in the hosts file, it doesn't write anything.

    I can see that b is without the ' but in the script, they are there. It's in the result of the command that they disappear. I replaced the ' with " but that didn't work either. Could that be the cause of the non-writing?
    Thank you.

    --
    Researcher in improvements.

    [url]
    0
    1. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
       
      Hello,

      Try it like this:
      sed -i -e '/MyHouse/{s/^[^\t]*/86.66.183.119/;b}' -e '$ a\86.66.183.119\tMyHouse' /etc/hosts
      0
  4. thierryR51 Posted messages 144 Status Member 8
     
    Indeed, post 29 also mentions the same thing and I had replied to you afterwards. It works but not well. Maybe we need to make 2 sed lines with an "if" condition?

    --
    Researcher in improvements.

    [url]
    0
    1. zipe31 Posted messages 34620 Registration date   Status Contributor Last intervention   6 501
       
      However...

      The entry "MaMaison" does not exist and is added:
      $ cat hosts 
      80.119.80.108 Backup

      $ sed -e '/MaMaison/{s/^[^\t]*/86.66.183.119/;b}' -e '$ a\86.66.183.119\tMaMaison' hosts
      80.119.80.108 Backup
      86.66.183.119 MaMaison


      The entry "MaMaison" exists and is modified:
      $ cat maison 
      80.119.80.108 Backup
      192.168.10.1 MaMaison

      $ sed -e '/MaMaison/{s/^[^\t]*/86.66.183.119/;b}' -e '$ a\86.66.183.119\tMaMaison' maison
      80.119.80.108 Backup
      86.66.183.119 MaMaison
      0