Sed command to replace text
Solved
thierryR51
Posted messages
144
Status
Membre
-
zipe31 Posted messages 34620 Registration date Status Contributeur Last intervention -
zipe31 Posted messages 34620 Registration date Status Contributeur Last intervention -
Hello,
I'm trying to replace an IP in the hosts file
I tried this:
But the result I get is this;
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]
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 réponses
Thank you Zipe31. You're still the great bash specialist...
Have a good evening.
--
Researcher in improvements.
[url]
Have a good evening.
--
Researcher in improvements.
[url]
1 line of hosts is broken down as follows:
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
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
I adapted it to my variables. In fact, it writes nothing. I only have the proofreading of my file.
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...
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...
Hello,
An example of a hosts file without the entry MaMaison
An example with both entries
The script file for sed :
The script executed on the file without the occurrence of MaMaison :
The script executed on the file already having both occurrences
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
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.
The return of the command is
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?
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?
Little problem. When sed wants to write
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]
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]
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]
--
Researcher in improvements.
[url]
However...
The entry "MaMaison" does not exist and is added:
The entry "MaMaison" exists and is modified:
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
sed -i.bak '/'$nameServeur'/{s/^[^ ]*/'$newIp'/}' /etc/hostsI 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....
nameServeur=${Serveurname%% }but it is still there.