LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-10-2007, 03:29 PM   #1
gctaylor1
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Rep: Reputation: 0
grep/sed/awk - find match, then match on next line


I'm trying to find the right combination of syntax to match first the hme0 and then the 192.168.* from the output of ifconfig -a shown below. It looks like GNU grep has the -A switch but I don't have GNU grep, I have to use the existing tools on a Solaris 10 machine. I've been playing around with sed and awk, but can't get the right combination.



Code:
 # ifconfig -a
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000 
lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone zone1
        inet 127.0.0.1 netmask ff000000 
lo0:2: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone bigzone
        inet 127.0.0.1 netmask ff000000 
hme0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 1500 index 2
        inet 192.168.21.19 netmask ffffff00 broadcast 192.168.21.255
        ether 8:0:20:ac:46:c9 
hme0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        zone zone1
        inet 192.168.21.163 netmask ffffff00 broadcast 192.168.21.255
hme0:2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        zone bigzone
        inet 192.168.21.166 netmask ffffff00 broadcast 192.168.21.255
This is what I was using and it worked great until I added zones and the additional IP addresses. If possible I'd like to incorporate a solution into this existing bit below.

Code:
if [ "$BASH" ]; then
   if [ "$EUID" -eq 0 ];
      then
         PS1="[\u@\h \W]\n # "
             PROMPT_COMMAND="echo -n [$(/sbin/ifconfig -a |  /usr/bin/awk '{print $2}'| grep 192\.168\.*)]"
        else
             PS1="[\u@\h \W]\n \$ "
             PROMPT_COMMAND="echo -n [$(/sbin/ifconfig -a | /usr/bin/awk '{print $2}'|  
grep  192\.168\.*)]"
        fi
   else :
fi
The reason for this is that the machine gets an IP address via DHCP and the IP address changes frequently.

Thanks,
Gary
 
Old 07-10-2007, 04:37 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Do you want just the IP address returned?
Code:
$ sed -n '/hme0: /,/inet/{
/inet/s/^ *inet \(192\.168\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\) .*/\1/p'} test
192.168.21.19
I cheated a bit assuming the 192.168. part was fixed.
I used the sed from cygwin to test it.

You might want to check if you need to use "\{1,3\}" or {1,3} for your version of sed. The file "test" I just cut and pasted from your post to test the sed command.

Last edited by jschiwal; 07-10-2007 at 04:40 PM.
 
Old 07-11-2007, 01:14 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
ifconfig -a  | awk '/^hme0: / {nextline=NR+1;next}
     { if ( NR==nextline ) { print "IP is: ",$2 } }
'
 
Old 07-11-2007, 08:55 AM   #4
gctaylor1
LQ Newbie
 
Registered: Dec 2006
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks jschiwal and ghostdog74. I couldn't get the first one to work. In cygwin I get an error sed: -e expression #1, char 85: unterminated `s' command and on Solaris I get a message about the command being garbled. Nonetheless it will be a good educational example to work through what it does and get it working. The second one works and that looks easy enough to put in my command as well as being educational too.

Thanks again for responding.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM
Need to strip words from front of line. sed/awk/grep? joadoor Linux - Software 6 08-28-2006 04:39 AM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 08:34 AM
grep [A-Z] & [a-z] match both upper and lower case. jschiwal Linux - General 4 05-28-2005 06:43 AM
how to grep only one string pr match gummimann Linux - General 3 11-06-2003 09:40 AM


All times are GMT -5. The time now is 10:12 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration