简体   繁体   中英

sed regex match and replace any last digit

I have lots of file containing following ipaddress, and i want to replace last digit of ip and look like i am having struggle to come up with correct regex

file1

IPADDR=10.30.2.26
NETMASK=255.255.0.0
GATEWAY=10.30.0.1

I want to replace 10.30.2.26 to 10.30.2.27 using sed but somehow i am missing something, i have tried following.

I have many file which i want to replace and last digit could be anything.

I have tried sed 's/[^IPADDR].$/7/g' file1

how do i match anything between ^IPADDR{anything}$ ?

In your regex, [^IPADDR] is a character class that search for any character except those listed between brackets. I'm not sure that's what you want.

You can use an address instead to find lines starting with IPADDR ( /^IPADDR/ ) and apply the substitution command on it:

sed '/^IPADDR/s/[0-9]$/7/' file

You may use the following command:

sed -r 's/(^IPADDR=[0-9.]+)([0-9]$)/\17/g' file

Prints:

IPADDR=10.30.2.27
NETMASK=255.255.0.0
GATEWAY=10.30.0.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM