-1

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
frank schmidt
  • 121
  • 3
  • 15

2 Answers2

1

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.

rr-
  • 14,303
  • 6
  • 45
  • 67
josifoski
  • 1,696
  • 1
  • 14
  • 19
0

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
Nathan Wilson
  • 856
  • 5
  • 12