Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

how to check condition and then pattern match

by laksh (Novice)
on Aug 23, 2012 at 10:04 UTC ( #989258=perlquestion: print w/ replies, xml ) Need Help??
laksh has asked for the wisdom of the Perl Monks concerning the following question:

My config.txt file look like this
eth0 Link encap:Ethernet HWaddr 00:0C:29:0F:9B:82 inet addr:192.168.8.76 Bcast:192.168.8.255 Mask:255.255.25 +5.0 inet6 addr: fe80::20c:29ff:fe0f:9b82/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:280252 errors:0 dropped:0 overruns:0 frame:0 TX packets:106184 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:35501675 (33.8 MiB) TX bytes:11696548 (11.1 MiB) Base address:0x1080 Memory:f4820000-f4840000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:4664 errors:0 dropped:0 overruns:0 frame:0 TX packets:4664 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:7952486 (7.5 MiB) TX bytes:7952486 (7.5 MiB)
my script file look like this
open FILE,"config.txt" or die "cannot open file : $!"; my $interface; # declare the variable while(<FILE>) { $interface = $1 if /^([\S]+)/; print "$interface => $1\n" if /inet addr:([0-9.]+)/; }
need help I need to check the condition if line contain "Link encap:" then get the same output as shown else its say "not ethernet" but its not happening
my output is: eth0 => 192.168.8.76 lo => 127.0.0.1

Comment on how to check condition and then pattern match
Select or Download Code
Re: how to check condition and then pattern match
by cheekuperl (Monk) on Aug 23, 2012 at 10:31 UTC
    What have you tried?
Re: how to check condition and then pattern match
by aitap (Deacon) on Aug 23, 2012 at 10:33 UTC
    Extend the second regexp to match only lines where "Ethernet" string is present. Something like this: ^([\S]+) ... Ethernet. See perlretut for more information.
    Sorry if my advice was wrong.
Re: how to check condition and then pattern match
by Kenosis (Priest) on Aug 23, 2012 at 15:23 UTC

    If you read the contents of your config.txt file into a variable, you can use a regex containing "Link encap:" to capture the IPs:

    use Modern::Perl; my $data = do { local $/; <DATA> }; my ( $eth0, $lo ) = $data =~ /Link encap:.+inet addr:([\d.]+).+inet addr:([\d.]+)/s; say $eth0 ? "eth0 => $eth0\nlo => $lo" : 'not ethernet'; __DATA__ eth0 Link encap:Ethernet HWaddr 00:0C:29:0F:9B:82 inet addr:192.168.8.76 Bcast:192.168.8.255 Mask:255.255.25 +5.0 inet6 addr: fe80::20c:29ff:fe0f:9b82/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:280252 errors:0 dropped:0 overruns:0 frame:0 TX packets:106184 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:35501675 (33.8 MiB) TX bytes:11696548 (11.1 MiB) Base address:0x1080 Memory:f4820000-f4840000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:4664 errors:0 dropped:0 overruns:0 frame:0 TX packets:4664 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:7952486 (7.5 MiB) TX bytes:7952486 (7.5 MiB)

    Output:

    eth0 => 192.168.8.76 lo => 127.0.0.1

    "not ethernet" will be printed if "Link encap:" is not in the config.txt contents.

    Hope this helps!

Re: how to check condition and then pattern match
by pvaldes (Chaplain) on Aug 23, 2012 at 15:32 UTC

    Seems that you want to parse an ifconfig output:

    use Net::Int::Stats;
Re: how to check condition and then pattern match
by afoken (Parson) on Aug 24, 2012 at 04:37 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://989258]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2014-11-25 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (148 votes), past polls