Skip to main content
deleted 1 character in body
Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97

With :

$ perl -ne '
    s/^(IPADDR=10\.2\.7\.)(\d+)/$2 <=< 255 and sprintf "%s%s", $1, $2 + 1/e;
    print
' file

Output:

DEVICE=eth0:1
NETMASK=255.255.255.0
IPADDR=10.2.7.149
BOOTPROTO=none
ONBOOT=yes
DNS1=10.2.53.150
PEERDNS=yes
DNS2=10.2.53.250
GATEWAY=10.2.7.1
TYPE=Ethernet
USERCTL=no
IPV6INIT=no

You can add -i switch to modify the file in-place.

With :

$ perl -ne '
    s/^(IPADDR=10\.2\.7\.)(\d+)/$2 <= 255 and sprintf "%s%s", $1, $2 + 1/e;
    print
' file

Output:

DEVICE=eth0:1
NETMASK=255.255.255.0
IPADDR=10.2.7.149
BOOTPROTO=none
ONBOOT=yes
DNS1=10.2.53.150
PEERDNS=yes
DNS2=10.2.53.250
GATEWAY=10.2.7.1
TYPE=Ethernet
USERCTL=no
IPV6INIT=no

You can add -i switch to modify the file in-place.

With :

$ perl -ne '
    s/^(IPADDR=10\.2\.7\.)(\d+)/$2 < 255 and sprintf "%s%s", $1, $2 + 1/e;
    print
' file

Output:

DEVICE=eth0:1
NETMASK=255.255.255.0
IPADDR=10.2.7.149
BOOTPROTO=none
ONBOOT=yes
DNS1=10.2.53.150
PEERDNS=yes
DNS2=10.2.53.250
GATEWAY=10.2.7.1
TYPE=Ethernet
USERCTL=no
IPV6INIT=no

You can add -i switch to modify the file in-place.

Source Link
Gilles Quénot
  • 36.7k
  • 7
  • 74
  • 97

With :

$ perl -ne '
    s/^(IPADDR=10\.2\.7\.)(\d+)/$2 <= 255 and sprintf "%s%s", $1, $2 + 1/e;
    print
' file

Output:

DEVICE=eth0:1
NETMASK=255.255.255.0
IPADDR=10.2.7.149
BOOTPROTO=none
ONBOOT=yes
DNS1=10.2.53.150
PEERDNS=yes
DNS2=10.2.53.250
GATEWAY=10.2.7.1
TYPE=Ethernet
USERCTL=no
IPV6INIT=no

You can add -i switch to modify the file in-place.