How to grep two IP addresses and increment the last number?
I am new to Linux and have written 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 that IP and the next one to see if they have any kind of presence there. Currently, I must 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 need to manually change the last digit from rwhois and grep. I got to a simple function like this
function info () {
rhowis -Br $1
grep -w '$1\|$1'
}
But of course I need to somehow increase the value of the last input by 1. Any good advice? And a little explanation of what you changed is appreciated so I can learn from that. Thank!
source to share
Just simply 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 packing (when is the last digit 255
), but this shouldn't be a problem if you don't need to handle broadcast addresses.
source to share
ip=$(awk -F\. '{ print $1"."$2"."$3"."($4+1) }' <<< $1)
With awk, you can set up the ip variable. We set the separator to "." and then take the original IP address, then print the first, second, and third separators along with the fourth incremented by one.
Then we will act:
grep -w '$1\|$ip'
source to share