Home > database >  How to replace specific field with sed between two paterns with other file content?
How to replace specific field with sed between two paterns with other file content?

Time:12-06

I want that file :

#ansible_python_interpreter=/usr/bin/python3 [win] #s1 192.168.1.148 #s2

to be like this :

#ansible_python_interpreter=/usr/bin/python3 [win] #s1 127.0.0.1 #s2

Im typing :

sed -e '/#s1/,/#s2/c\#s1\n127.0.0.12\n\#s2' hosts

OK, but I want the address from another file called tmp_ip : 10.0.0.1 for example but it can be any address present in it.

What could i do ?

CodePudding user response:

Using sed

$ sed -E "s/([0-9] \.[0-9] ?) /$(cat tmp_ip)/" input_file
#ansible_python_interpreter=/usr/bin/python3 [win] #s1 127.0.0.1 #s2

Or if the pattern must be matched

$ sed -E "s/(#s1 )[^#]*/\1$(cat tmp_ip) /" input_file
#ansible_python_interpreter=/usr/bin/python3 [win] #s1 127.0.0.1 #s2
  • Related