Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Need help to parse the text

by tachyon (Chancellor)
on Jun 17, 2004 at 04:29 UTC ( [id://367528]=note: print w/replies, xml ) Need Help??


in reply to Need help to parse the text

Your first problem is that the format of ifconfig -a may be different on different platforms. For example:

[root]# uname -s Linux [root]# ifconfig -a eth0 Link encap:Ethernet HWaddr 00:C0:9F:XX:XX:XX inet addr:64.x.x.x Bcast:64.x.x.x Mask:255.255.254.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:193891504 errors:0 dropped:0 overruns:0 frame:0 TX packets:153456331 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:1718775511 (1639.1 Mb) TX bytes:3265401055 (3114.1 + Mb) Interrupt:16 Base address:0xecc0 Memory:fe100000-fe120000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:56540745 errors:0 dropped:0 overruns:0 frame:0 TX packets:56540745 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2665326103 (2541.8 Mb) TX bytes:2665326103 (2541.8 + Mb)

Anyway you need to find a good way to chunkify the data into records. Assuming you get it in a scalar split "\n\n" should probably do the trick. Then the first string in your chunk is the interface name and m/status:\s+(\S+)/ will give your status. That won't work on Linux.

for my $chunk (split "\n\n", $data) { my ($if) = $chunk =~ m/^(\S+)/; my ($status) = $chunk =~ m/status:\s*(\S+)/; print "$if: $status\n"; }

cheers

tachyon

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://367528]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-19 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found