Saturday, November 13, 2021

[SOLVED] About the usage of sed awk grep

Issue

Everyone, what command line should I use to achieve this effect?

This is trying to use awk and sed but failed. Please advise.

Original:
server=/example-a.com/127.0.0.1#5353
ipset=/example-a.com/router
server=/example-b.com/127.0.0.1#5353
ipset=/example-b.com/router
server=/example-c.com/127.0.0.1#5353
ipset=/example-c.com/router

Achieve effect:
server=/example-a.com/127.0.0.1#5353
server=/example-a.com/127.0.0.2#5354
ipset=/example-a.com/router
server=/example-b.com/127.0.0.1#5353
server=/example-b.com/127.0.0.2#5354
ipset=/example-b.com/router
server=/example-c.com/127.0.0.1#5353
server=/example-c.com/127.0.0.2#5354
ipset=/example-c.com/router

Solution

Since you need arithmetic, I would go with awk:

awk -F'[#.]' '/^server/ { print $0, ORS $1"."$2"."$3"."$4"."$5+1"#"$6+1 } !/^server/'

Output:

server=/example-a.com/127.0.0.1#5353 
server=/example-a.com/127.0.0.2#5354
ipset=/example-a.com/router
server=/example-b.com/127.0.0.1#5353 
server=/example-b.com/127.0.0.2#5354
ipset=/example-b.com/router
server=/example-c.com/127.0.0.1#5353 
server=/example-c.com/127.0.0.2#5354
ipset=/example-c.com/router


Answered By - Thor