grep/sed/awk - find match, then match on next line
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
$ 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.