Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-10-2007, 03:29 PM   #1
Registered: Dec 2006
Distribution: Red Hat
Posts: 45

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.

 # ifconfig -a
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet netmask ff000000 
lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone zone1
        inet netmask ff000000 
lo0:2: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        zone bigzone
        inet netmask ff000000 
hme0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 1500 index 2
        inet netmask ffffff00 broadcast
        ether 8:0:20:ac:46:c9 
hme0:1: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        zone zone1
        inet netmask ffffff00 broadcast
hme0:2: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        zone bigzone
        inet netmask ffffff00 broadcast
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.

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

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

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Do you want just the IP address returned?
$ sed -n '/hme0: /,/inet/{
/inet/s/^ *inet \(192\.168\.[[:digit:]]\{1,3\}\.[[:digit:]]\{1,3\}\) .*/\1/p'} test
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
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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.


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

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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 AM.

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