Register
It is currently Wed Jul 23, 2014 12:04 pm

using sed/awk to grab the first three octets of an IP addr


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Thu Oct 27, 2011 11:16 am   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
Hello,

I am reaching out to the pro's here. In one of my network bash scripts, I need to be able to print only the first three octets of on IP address. Here is what I have so far:
Code:
IP_3OCT=`ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}' | sed /127.0.0.1/d | cut -c 1-11`

This would trim off the last octet for 192.168.10.100, giving me:
192.168.10.

but I need this to be universal enough to handle any class of IP address...say a class A IP of 10.100.15.10? This line would give me:
10.100.15.1

I still need that last "1" trimmed. I am sure there is a better way to do this, so I don't have to modify the script itself each and every time I need to do this task.

btw...if anyone was wondering why I am removing the loopback, or why it is even coming up at all...this is the only way I could figure out how to get just the static IP from the ifconfig. If there a better way for that, then I am all ears (ps...I tried using hostname -i, but when this script runs, the hosts file isn't set up yet and this defaults to the loopback).

Any help is most appreciated!


Top
 Profile  
 PostPosted: Thu Oct 27, 2011 12:35 pm   

Joined: Tue Apr 19, 2011 11:01 pm
Posts: 36
well, I think I solved it:
Code:
IP_3OCT=`ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}' | sed /127.0.0.1/d | cut -d "." -f1-3


this will give me the first three octets (only missing the final ".", but I can live with that and put that into where I echo the variable.

But, I am sure I wrote this string way too long, so if anyone has a way to shorten it and get the same results, I would love to learn.


Top
 Profile  
 PostPosted: Thu Oct 27, 2011 7:49 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Code:
ifconfig eth0| awk -F ' *|:' '/inet ad*r/{split($4,a,"\\."); printf("%d.%d.%d\n", a[1],a[2],a[3])}'
don't pipe grep to awk, and then to sed, or/and cut. If you think about such an awful thing, remember that awk can do the job alone.


Top
 Profile  
 PostPosted: Fri Oct 28, 2011 11:45 am   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
Practically the same as Watael, but slightly shorter:
Code:
ifconfig eth0 | awk -F: '/inet addr:/ { split($2,a,"."); print a[1]"."a[2]"."a[3]; }'


Top
 Profile  
 PostPosted: Fri Oct 28, 2011 2:26 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Code:
ifconfig eth0 | sed -nr '/inet addr:/{s/ *inet addr:([^ ]*)\..*/\1/;p}'

or
Code:
ifconfig eth0 | grep 'inet addr:' | cut -f2 -d: | cut -f1-3 -d.

or
Code:
ifconfig eth0 | perl -nE '$,=".";say((split/[:.]/)[1..3])if/inet addr:/'

...
:-B


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP