Hi Jack,

Usually /usr/bin/sed command work fine with each sub-command in separate line, 
in your testing for example:

#more test.sh

#!/bin/sh
ifconfig -a | /usr/bin/sed -n '/BROADCAST.*IPv4/{
n;
p
}'

#./test,sh
#        inet 10.10.10.10 netmask ffffff00 broadcast 10.10.10.255

Hopefully it can help.

Zhongyuan



---- 在 Mon, 25 Oct 2010 17:55:59 -0700 Jack Schwartz 
<[email protected]> 写道 ---- 

 Hi everyone. 
 
I'm trying to do a sed one-liner but can't seem to get it to work... 
 
I want to parse ifconfig -a output and print the line which follows the 
line with the regular expression of "BROADCAST.*IPv4". End goal is to 
get the IP Address and netmask of the first non-loopback IPv4 interface. 
 
So when I see this: 
--- 
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 
8232 index 1 
 inet 127.0.0.1 netmask ff000000 
e1000g0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 
1500 index 3 
 inet 10.132.145.68 netmask fffffe00 broadcast 10.132.145.255 
lo0: flags=2002000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv6,VIRTUAL> mtu 
8252 index 1 
 inet6 ::1/128 
e1000g0: flags=20002004841<UP,RUNNING,MULTICAST,DHCP,IPv6> mtu 1500 index 
3 
 inet6 fe80::223:18ff:fe72:3644/10 
schwa...@jslaptop:~/test$ ifconfig -a | grep BROADCAST | grep IPv4 
e1000g0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 
1500 index 3 
--- 
 
I want to output this: 
 inet 10.132.145.68 netmask fffffe00 broadcast 10.132.145.255 
 
Everywhere I google I see that the following should work: 
 ifconfig -a | /usr/bin/sed -n '/BROADCAST.*IPv4/{n;p}' 
 
but I get an error with /usr/bin/sed instead: 
 sed: command garbled: /BROADCAST.*IPv4/{n;p} 
 
More confusing to me is when I remove the {} I get too much output: 
 
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 
8232 index 1 
 inet 127.0.0.1 netmask ff000000 
 inet 10.132.145.68 netmask fffffe00 broadcast 10.132.145.255 
lo0: flags=2002000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv6,VIRTUAL> mtu 
8252 index 1 
 inet6 ::1/128 
e1000g0: flags=20002004841<UP,RUNNING,MULTICAST,DHCP,IPv6> mtu 1500 index 
3 
 inet6 fe80::223:18ff:fe72:3644/10 
 
(actually, this is all the lines except the one I am searching for) 
 
When I run gnu sed, then it works. 
 
ifconfig -a | /usr/gnu/bin/sed -n '/BROADCAST.*IPv4/ {n;p}' 
 inet 10.132.145.68 netmask fffffe00 broadcast 10.132.145.255 
 
Questions: 
1) Is using gnu sed a problem if this code is for an auto-installer project? 
2) What am I doing incorrectly above? How do I "un-garble" the 
/usr/bin/sed command? 
 
 Thanks, 
 Jack 
_______________________________________________ 
caiman-discuss mailing list 
[email protected] 
http://mail.opensolaris.org/mailman/listinfo/caiman-discuss 

_______________________________________________
caiman-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/caiman-discuss

Reply via email to