Edit line a textfile using linux shell

91 Views Asked by At

I need to change an IP-address in a text-file using linux-shell. How can I do that? Do you know a great source for regular expressions?

the textfile.txt contains sth like this:

# text text ffeefe 
Whatever text text ffeefe 
# text text ffeefe 
#
IPAddress : 192.168.200.40
#
# text text ffeefe
#
Whatever text text ffeefe 
# text text ffeefe 

This is what I am looking for:

sed -i / find XXX.XXX.XXX.XXX / 192.168.200.41 / textfile.txt

The IP could be anything in that format XXX.XXX.XXX.XXX e.g. 192.168.200.40

2

There are 2 best solutions below

0
On BEST ANSWER

Although fedorqui is right with asking more information about occurring of IP after specific content, I'll provide (easy) solution for finding any IPv4 string.

The expression is:

[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+

using it in sed looks like this:

sed -i -r 's/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/newip/' file

On the other hand, if this is fixed "IPAddress : " before IP, then the OP should just use the IP they want.

0
On

This will find line beginning with IPAddress : and change the digits to the new IP.

sed -i -r '/^IPAddress :/s/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/192.168.200.41/' textfile.txt