Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Awk/grep

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-06-2013
Lotheovian Lotheovian is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 19 September 2013, 5:41 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Awk/grep

Basically what I'm trying to do is pull the first three octets from the ipaddr out of ifconfig


Code:
ifconfig eth0 |awk -F" " '{ print $2 }'|grep addr: |awk -F":" '{ print $2 }'| awk -F"." '{print $1"."$2"."$3}'

keeps giving me


Code:
192.168.0
..

and I can not for the life of me get rid of the bottom half (the ..'s)

Last edited by Scott; 07-06-2013 at 10:17 PM.. Reason: Please use code tags and a descriptive subject title next time
Sponsored Links
    #2  
Old Unix and Linux 07-06-2013
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 May 2015, 9:22 AM EDT
Location: Zürich
Posts: 8,552
Thanks: 337
Thanked 1,076 Times in 935 Posts
You could probably add one more pipeline:

Code:
... | head -1

But there may be a shorter way depending on your OS.

I.e. in Redhat:

Code:
$ hostname -I

Which OS are you using?
Sponsored Links
    #3  
Old Unix and Linux 07-06-2013
Lotheovian Lotheovian is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 19 September 2013, 5:41 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
root@bt:~# ifconfig eth0 |grep "inet addr" |awk -F":" '{print$2}'|awk -F"." '{print $1"."$2"."$3}'
192.168.0
root@bt:~# inet=$?
root@bt:~# echo "$inet"
0


my head is going to explode

---------- Post updated at 09:40 PM ---------- Previous update was at 09:39 PM ----------

Backtrack 5r3

Ubuntu

Last edited by Scott; 07-06-2013 at 10:42 PM.. Reason: Code tags
    #4  
Old Unix and Linux 07-06-2013
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 May 2015, 9:22 AM EDT
Location: Zürich
Posts: 8,552
Thanks: 337
Thanked 1,076 Times in 935 Posts
You're testing the result of the command with $?, not the output. If you want to store the output:

Code:
inet=$(...)

i.e.

Code:
$ inet=$(ip addr show eth0 | awk -F"[ /]" '/inet /{print $6}')
$ echo $inet

Sponsored Links
    #5  
Old Unix and Linux 07-06-2013
Lotheovian Lotheovian is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 19 September 2013, 5:41 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
I'm trying to trim off that last octet so I can input the variable inet.* and run a nmap on specific ports.

---------- Post updated at 09:48 PM ---------- Previous update was at 09:46 PM ----------

Wait a tick, I see what you did. Thank you!
Sponsored Links
    #6  
Old Unix and Linux 07-06-2013
Scott's Unix or Linux Image
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 23 May 2015, 9:22 AM EDT
Location: Zürich
Posts: 8,552
Thanks: 337
Thanked 1,076 Times in 935 Posts

Code:
# ip addr show eth0 | awk -F"[ /.]" '/inet /{print $6"."$7"."$8}'
10.10.44

Sponsored Links
    #7  
Old Unix and Linux 07-06-2013
Lotheovian Lotheovian is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 19 September 2013, 5:41 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
root@bt:~# inet=$(ifconfig eth0 |grep "inet addr" |awk -F":" '{print$2}'|awk -F"." '{print $1"."$2"."$3}')
root@bt:~# echo $inet
192.168.0

I've just started using awk. At the moment I only know how to grab fields. So the fancy things you're doing, I have no idea. This works though, thank's a bunch!

Moderator's Comments:
Please use code tags

Last edited by Scott; 07-06-2013 at 10:53 PM.. Reason: Code tags
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK/GREP: grep only lines starting with integer chrisjorg Shell Programming and Scripting 2 04-17-2012 10:06 AM
Read content between xml tags with awk, grep, awk or what ever... Sebi0815 Shell Programming and Scripting 5 03-10-2010 05:58 PM
Is it better to grep and pipe to awk, or to seach with awk itself DeCoTwc Shell Programming and Scripting 4 10-07-2008 02:52 PM
MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else hariza Shell Programming and Scripting 4 09-18-2008 02:56 AM
[grep awk cut] > awk firdousamir Shell Programming and Scripting 6 08-02-2007 04:22 AM



All times are GMT -4. The time now is 09:22 AM.