Skip to content
Advertisement

How to grep two IP addresses and increase the value of the last number?

I’m pretty new to Linux and writing scripts etc. I have this task where I need to find an IP-address from a database and then grep a bunch of files with this IP and the next one to see, if they have any presence there. Currently I have to first write:

rwhois -Br 0.0.0.0

and then

grep -wl '0.0.0.0|0.0.0.1' /path/to/some/files

And I have to manually change the last digit from the rwhois and from the grep. I got as far as to write a simple function like this

function info () {
    rhowis -Br $1
    grep -w '$1|$1'
}

But of course I’d have to somehow increase the value of the latter input by 1. Any good advice? And a small explanation of what you changed is appreciated so I can learn from this. Thanks!

Advertisement

Answer

It’s simple to just increase the last digit with awk:

info() {
    local ip="$1"
    local nextip=$(awk -F. '{ print $1 "." $2 "." $3 "." ($4+1) }' <<<"$1")
    rhowis -Br "$ip"
    grep -w "$ip|$nextip'
}

Note that this will not handle wrapping (when the last digit is 255), but that shouldn’t be a problem if you don’t need to handle broadcast addresses.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement