Billy Holmes schrieb:
> brullo nulla wrote:
>> Wow. You evil geniuses of regular expressions! °_°
>
> can even shorten it with just one sed expression:
>
> /sbin/ifconfig eth0 | sed -n 's/^.*inet addr:\([^ ]*\) .*$/\1/p'
Doesn't work, as it makes wrong assumptions. It assumes, that
there's string "inet addr:". For example in LANG=de_DE, that's
wrong:
[EMAIL PROTECTED] ~ $ /sbin/ifconfig ra0
ra0 Protokoll:Ethernet Hardware Adresse 00:14:A5:00:AE:D2
inet Adresse:192.168.1.11 Bcast:192.168.1.31 Maske:255.255.255.224
UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1
RX packets:12234 errors:0 dropped:0 overruns:0 frame:0
TX packets:6965 errors:0 dropped:0 overruns:0 carrier:0
Kollisionen:11 Sendewarteschlangenlänge:1000
RX bytes:18131234 (17.2 Mb) TX bytes:665533 (649.9 Kb)
Interrupt:5
To fix that, the locale should be shanged, eg. like this:
$ LC_ALL=C /sbin/ifconfig ra0
Let's make a different assumption, which is independent of
the locale - IP is after the 1st : in the 2nd line. One
solution:
[EMAIL PROTECTED] ~ $ /sbin/ifconfig ra0 | head -n 2 | tail -n 1 | sed
's|.*:\(.*\) .*:.*:.*|\1|'
192.168.1.11
Doing away with "tail -n 1", as sed can do that:
[EMAIL PROTECTED] ~ $ /sbin/ifconfig ra0 | head -n 2 | sed -e '$!d' -e
's|.*:\(.*\) .*:.*:.*|\1|'
192.168.1.11
"head" is also useless:
[EMAIL PROTECTED] ~ $ /sbin/ifconfig ra0 | sed -n 2p | sed -e '$!d' -e
's|.*:\(.*\) .*:.*:.*|\1|'
192.168.1.11
But I actually don't quite like the regexp - it's too long... With
perl, I'd use a "non-gready" .*, like so:
[EMAIL PROTECTED] ~ $ /sbin/ifconfig ra0 | sed -n 2p | sed -e '$!d' | perl -pe
's|.*?:(.*?) .*|$1|'
192.168.1.11
How to do it more ellegant?
Alexander Skwar
--
[email protected] mailing list