tso tso - 4 months ago 19
Bash Question

Write an IP with Netmask (sed)

I have a problem. I want to write an IP into a file with

sed


newsource=1.2.3.4/24
sed -i 's/.*#source*/&\n'"$newsource"'/' file


$newsource
is an IP, which CAN contain a net-mask
/24
or not. Right now the
sed
writes the full IP but not the
/24
. How can I change that?

Answer

This is because you must either escape your /, or change the sed separator to something else:

escape: \/

newsource=1.2.3.4\/24
sed -i 's/.*#source*/&\n'"$newsource"'/' file

or

change sed separator to ~

newsource=1.2.3.4/24
sed -i 's~.*#source*~&\n'"$newsource"'~' file

Share and enjoy.

Comments