Bash subtract fourth octet of an IP by 1

Hello,

Im looking to help out my team by automating a simple search list. The user will look for a peering ip /30. For example 192.168.1.2/30 and gets the result. Im trying to get the entered /30 and subtract the last octet by one.

echo -n "Enter peering ip : "; read peeringip
        cat /shared_script/folder/R1.router.txt | grep -i $peeringip
showing requested output
        echo "$peeringip-1"

(but nothing happens and I get is 217.30.80.110-1) I have trued to subtract also by

ournum = $peering -1

( nothing either)

Can someone help me out here?

Hello D'go,

Please use code tags for your posts, following may help you in same. Kindly try to show us some sample inputs
and expected output from your side, it will be good for us to understand your requirement more closely.

Let's say we have a input file with following details.

cat ip_list
192.168.1.2/30
192.168.1.2/35
192.168.1.2/40
 

Now we can use following script named checkip.ksh to get this done as follows.

cat checkip.ksh
echo "Enter ip:"
read IP_val
awk -F"/" -vvalip="$IP_val" '{if($NF==valip){split($1, A,".");A[4]-=1;VAL=A[1] OFS A[2] OFS A[3] OFS A[4]}} END{print VAL}' OFS="." ip_list
 

Execution and output will be as follows.

./checkip.ksh
Enter ip:
30
192.168.1.1
 

Hope this helps you. Enjoy learning :b:

Thanks,
R. Singh

How about

awk -F"[./]" '$NF==30{sub("."$4"/","."$4-1"/")}1' file
192.168.1.1/30
192.168.1.2/35
192.168.1.2/40

going into a meeting - will check them shortly. There is no need to for the / - the user should know what IP or part of the /30 to enter.