LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   grep/sed/awk - find match, then match on next line (https://www.linuxquestions.org/questions/programming-9/grep-sed-awk-find-match-then-match-on-next-line-568201/)

gctaylor1 07-10-2007 03:29 PM

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

jschiwal 07-10-2007 04:37 PM

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.

ghostdog74 07-11-2007 01:14 AM

Code:

ifconfig -a  | awk '/^hme0: / {nextline=NR+1;next}
    { if ( NR==nextline ) { print "IP is: ",$2 } }
'


gctaylor1 07-11-2007 08:55 AM

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.


All times are GMT -5. The time now is 11:54 PM.