Update:use strict; use warnings; use Test::More tests => 2; my $re_dn = qr{^(\w+) (\d+) (\d{2}:\d{2}:\d{2}) .*: eth(\d): link down +}; my $re_up = qr{^(\w+) (\d+) (\d{2}:\d{2}:\d{2}) .*: eth(\d): link up, +(\d+)Mbps, ([^,]+), lpa (\w+)}; my @log = ( 'May 19 13:58:01 foo: eth0: link down', 'May 19 13:58:11 foo: eth0: link up, 100Mbps, bar, lpa quux' ); for (@log) { if (my ( $mon, $day, $time, $interface ) = /$re_dn/) { is $time, '13:58:01'; next; } if (my ( $mon, $day, $time, $interface, $rate, $duplex, $lpa ) = / +$re_up/) { is $time, '13:58:11'; next; } }
I parse a lot of printouts that are just intended for humans to read. There is kind of an "art" to that. And the result is always inherently unstable because there is no defined spec. This looks to be a lot more constrained because this appears to be some kind of standard program generated log.
Update 2:
In my opinion, the names $re_dn and $re_up are one step "too clever". I probably would have used $re_link_down and $re_link_up. Using some extra characters in the name costs essentially nothing in compile speed. And I guess if you have a really fancy editor or IDE with auto complete, nothing much in typing either. This is a style issue. I mention it because details like this can matter when you re-visit code that you wrote X years ago.
use strict; use warnings; my @log = ( 'May 18 12:01:01 bar: eht1 BS', 'May 19 13:58:01 foo: eth0: link down', 'May 19 13:58:11 foo: eth0: link up, 100Mbps, bar, lpa quux' ); foreach my $line (@log) { next unless (index($line, ": link")>=0); # skips BS # this is very fast # even running twice for # link up or link down is +fast print "$line\n"; # focus on link mentioning lines here } #May 19 13:58:01 foo: eth0: link down #May 19 13:58:11 foo: eth0: link up, 100Mbps, bar, lpa quux
In reply to Re^2: scan, match and extract
by Marshall
in thread scan, match and extract
by seismofish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |