Bash subtract fourth octet of an IP by 1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash subtract fourth octet of an IP by 1
# 1  
Old 09-03-2015
Network 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.


Code:
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
Code:
ournum = $peering -1

( nothing either)

Can someone help me out here?

Last edited by Scrutinizer; 09-03-2015 at 08:52 AM.. Reason: code tags
# 2  
Old 09-03-2015
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.
Code:
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.
Code:
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.
Code:
./checkip.ksh
Enter ip:
30
192.168.1.1

Hope this helps you. Enjoy learning Smilie

Thanks,
R. Singh
# 3  
Old 09-03-2015
How about
Code:
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

# 4  
Old 09-03-2015
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Help with how to remove 4rth octet :

Experts, In one example I have seen how to get output upto 3rd octet, when there is a ":" separated with the 4rth octet. However in this example how to remove 4rth octet and to keep upto 3rd octet with regular expressions and awk sub function: I have tried with :but not working: # awk '{... (3 Replies)
Discussion started by: rveri
3 Replies

2. Shell Programming and Scripting

awk Quick Help: printing upto 3rd octet .

Hi Experts, I am trying to print $2 & the IP_address upto 3rd octet only. But unable to do so, Trying # awk '{print $2, substr($4,1,9)}' file . but not correct File: HOST= cmiHOST06 :: 10.26.107.73:/data120 /nbu/cmiHOST06/athpx07/aa1 HOST= cmiHOST05 :: 10.26.12.76:/data120... (5 Replies)
Discussion started by: rveri
5 Replies

3. Shell Programming and Scripting

replace by match on fourth column

Hi friends, My input file is this way chr1 100 200 "abc" chr1 350 400 "abc" chr2 450 600 "def" chr2 612 780 "def" How do I make this file into chr1 100 400 "abc" chr2 450 780 "def" This is basically matching on the fourth column and taking the minimum of second column and the... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

4. UNIX for Dummies Questions & Answers

What is an (application/octet-stream) file?

I'm trying to learn as much about GRUB as I can and it's stages are stored in these types of files. Any info or search terms is appreciated!:wall: (5 Replies)
Discussion started by: theKbStockpiler
5 Replies

5. Shell Programming and Scripting

Substring between third and fourth occurrence

Hi , Please help me to get the data extracted between the 3rd and 4th dot of a transaction file. Source data TRANS,ARRIVED,ABC.1Gt.CDRFLOW123.MAINFRAMES.SYS.tXT/ARRIVED,TRANS/CDRFLOW123.MAINFRAMES.SYS.tXT/ARRIVED,TRANS ... (1 Reply)
Discussion started by: wangkc
1 Replies

6. Shell Programming and Scripting

How can I delete every third AND fourth line in a file?

cat test.nmea|awk 'NR%3!=0' This deletes the 3rd line, or I can delete the fourth but I can't figure out how to delete the 3rd and 4th together. I'm looking for a quick way to make a GPS log half its size. Also how do I pipe the output to another file? Hope someone can help! (5 Replies)
Discussion started by: traveltrousers
5 Replies

7. Shell Programming and Scripting

fourth field is number in a line

hi i hav a file like 121212 asdd d 7 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 5 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 5 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 4 dfsdffdffsdfsdfsdfdf rrretrtrtre 121212 asdd d 6 dfsdffdffsdfsdfsdfdf rrretrtrtre i need to... (4 Replies)
Discussion started by: Satyak
4 Replies

8. UNIX and Linux Applications

Any idea on 3 Octet IP address ?

Hi All, I found my weblog contain entries like 121.23.3 Instead of four octet. I am quite confused is it possible to have 3 octet ip at all ?? Is it generating by any program and hittng the website ? Is it a subdomain ? Please tell me your understanding on it ? Thanks (4 Replies)
Discussion started by: jambesh
4 Replies

9. Programming

ip address octet increments

Hi all, Situation is as below. I would get an IP address and port from eithe r a file or command line. It probably would be as char * or string. So was wondering how I could accept this and increment the last octets? Incrementing the port is fine. I could get that into an integer by atoi()... (8 Replies)
Discussion started by: Naanu
8 Replies
Login or Register to Ask a Question