Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: scan, match and extract

by BillKSmith (Monsignor)
on May 19, 2022 at 20:48 UTC ( [id://11144018]=note: print w/replies, xml ) Need Help??


in reply to scan, match and extract

Matching parts of the line separately has several advantages. No matching is duplicated. Individual regexes are shorter and extract fewer parameters. Uninteresting lines are discarded sooner. All variables are properly scoped.
use strict; use warnings; my $time_stamp = qr{^(\w+) (\d+) (\d{2}:\d{2}:\d{2})}; while (<DATA>) { next if !(my($mon, $day, $time) = /$time_stamp/); next if !(my($interface, $updown) = $' =~ /.*:\seth(\d): link (up| +down)/); if ( $updown eq 'down' ) { #... # Process down print $time, "\n"; } else { my( $rate, $duplex, $lpa ) = $' =~ /,(\d+)Mbps, ([^,]+), lpa ( +\w+)/; next if !defined($lpa); # Invalid 'up' probably never happens. #... # Process up print $lpa, "\n"; } } __DATA__ May 03 10:15:21 some text: eth3: link down May 04 10:16:23 some text: eth3: link up,562Mbps, foos, lpa fum no time

UPDATE: Refer to @LAST_MATCH_START and @LAST_MATCH_END for documentation on $'.

Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144018]
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-03-29 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found