sed to replace a field from a line with another field

i have something like this,

cat filename.txt

hui this si s"dfgdfg" omeone ipaddress="10.19.123.104" wel hope this works

i want to replace only 10.19.123.104 with different ip say 10.19.123.103

i tried this

 sed -i "s/'ipaddress'/'ipaddress=10.19.123.103'/g" filename.txt

output should be like this

hui this si s"dfgdfg" omeone ipaddress="10.19.123.103" wel hope this works

its not working.. i think my command is wrong.. can anyone plz help

---------- Post updated at 08:24 PM ---------- Previous update was at 08:14 PM ----------

got it finally :slight_smile:

 sed 's/ipaddress=\"$/ipaddress=10.19.123.103/' filename

Your sed regular expression is only matching 'ip_address' (with quotes, which won't actually match anything in your input) and not the actual address.

Try:

sed -i 's/ipaddress="[^"]*"/ipaddress="10.19.123.103"/g' filename.txt

( [^"]* matches 0 or more of any character except double-quote)

1 Like