I have two variables,
JavaScript
x
first= new pool ser/@10.25.89.322
second=ESX_10.2.3.4@10.52.33.21
i want to replace ESX_10.2.3.4@10.52.33.21
with new pool ser/@10.25.89.322
and also want to remove space before new word.
i have tried like,
JavaScript
sed -i -e "s/${second}/"${first//\/\\}"/g"
and
JavaScript
sed -i -e "s/${second}/"${first//\[ ]*/\\}"/g"
sed -i -e "s/${second}/"${first//\ */ *\\}"/g"
I don’t want to delete space ,just want to replace as it is(except space before “new”).
Advertisement
Answer
Give a try to this:
JavaScript
sed -i "/${second}/ {s:${second}:${first//\/\\}:g ; s/ new /new /}"
It is removing the space
in the first occurrence of new
in the line after replacement.
Also, in the test below, double-quote were added:
JavaScript
$ first=" new pool ser/@10.25.89.322"
$ second=ESX_10.2.3.4@10.52.33.21
$ sed -i "/${second}/ {s:${second}:${first//\/\\}:g ; s/ new /new /}" mytestfile.txt