1 |
Billy Holmes schrieb: |
2 |
> brullo nulla wrote: |
3 |
>> Wow. You evil geniuses of regular expressions! °_° |
4 |
> |
5 |
> can even shorten it with just one sed expression: |
6 |
> |
7 |
> /sbin/ifconfig eth0 | sed -n 's/^.*inet addr:\([^ ]*\) .*$/\1/p' |
8 |
|
9 |
Doesn't work, as it makes wrong assumptions. It assumes, that |
10 |
there's string "inet addr:". For example in LANG=de_DE, that's |
11 |
wrong: |
12 |
|
13 |
alexander@blatt ~ $ /sbin/ifconfig ra0 |
14 |
ra0 Protokoll:Ethernet Hardware Adresse 00:14:A5:00:AE:D2 |
15 |
inet Adresse:192.168.1.11 Bcast:192.168.1.31 Maske:255.255.255.224 |
16 |
UP BROADCAST NOTRAILERS RUNNING MULTICAST MTU:1500 Metric:1 |
17 |
RX packets:12234 errors:0 dropped:0 overruns:0 frame:0 |
18 |
TX packets:6965 errors:0 dropped:0 overruns:0 carrier:0 |
19 |
Kollisionen:11 Sendewarteschlangenlänge:1000 |
20 |
RX bytes:18131234 (17.2 Mb) TX bytes:665533 (649.9 Kb) |
21 |
Interrupt:5 |
22 |
|
23 |
To fix that, the locale should be shanged, eg. like this: |
24 |
|
25 |
$ LC_ALL=C /sbin/ifconfig ra0 |
26 |
|
27 |
Let's make a different assumption, which is independent of |
28 |
the locale - IP is after the 1st : in the 2nd line. One |
29 |
solution: |
30 |
|
31 |
alexander@blatt ~ $ /sbin/ifconfig ra0 | head -n 2 | tail -n 1 | sed 's|.*:\(.*\) .*:.*:.*|\1|' |
32 |
192.168.1.11 |
33 |
|
34 |
Doing away with "tail -n 1", as sed can do that: |
35 |
|
36 |
alexander@blatt ~ $ /sbin/ifconfig ra0 | head -n 2 | sed -e '$!d' -e 's|.*:\(.*\) .*:.*:.*|\1|' |
37 |
192.168.1.11 |
38 |
|
39 |
"head" is also useless: |
40 |
|
41 |
alexander@blatt ~ $ /sbin/ifconfig ra0 | sed -n 2p | sed -e '$!d' -e 's|.*:\(.*\) .*:.*:.*|\1|' |
42 |
192.168.1.11 |
43 |
|
44 |
But I actually don't quite like the regexp - it's too long... With |
45 |
perl, I'd use a "non-gready" .*, like so: |
46 |
|
47 |
alexander@blatt ~ $ /sbin/ifconfig ra0 | sed -n 2p | sed -e '$!d' | perl -pe 's|.*?:(.*?) .*|$1|' |
48 |
192.168.1.11 |
49 |
|
50 |
How to do it more ellegant? |
51 |
|
52 |
Alexander Skwar |
53 |
-- |
54 |
gentoo-user@g.o mailing list |