Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Some assistance with splitting variables

by kcott (Canon)
on Apr 06, 2013 at 06:17 UTC ( #1027253=note: print w/ replies, xml ) Need Help??

in reply to Some assistance with splitting variables

This captures the data in the format you want:

$ perl -Mstrict -Mwarnings -E ' use constant { ACTION => 0, FAN_A => 1, FAN_B => 2, SEND => 3, FAILURE => 4, }; my $re = qr{ \A \d+ \s+ (?<date> \S+ ) .*? ACTION=< (?<ACTION> [^>]+ ) > \s+ FAN_A=< (?<FAN_A> [^>]+ ) > \s+ FAN_B=< (?<FAN_B> [^>]+ ) > \s+ SEND=< (?<SEND> [^>]+ ) > \s+ FAILURE=< (?<FAILURE> [^>]+ ) }x; my $log_line = q{19476 2013-04-05,12:10:51.909293 main INFO Running normally with ACTION=<processin +g> FAN_A=<OK> FAN_B=<OK> SEND=<Sent mail (221 2.0.0 Service closing t +ransmission channel)> FAILURE=<2>}; $log_line =~ /$re/; my ($date, @info) = @+{qw{date ACTION FAN_A FAN_B SEND FAILURE}}; say $date; say $info[ACTION]; say $info[FAN_A]; say $info[FAN_B]; say $info[SEND]; say $info[FAILURE]; ' 2013-04-05,12:10:51.909293 processing OK OK Sent mail (221 2.0.0 Service closing transmission channel) 2

This is just a commandline proof-of-concept. Your real-world application would probably look more like:

... use constant { ... my $re = qr{ ... open my $log_fh, '<', $logfile or die $!; while my $log_line (<$log_fh>) { $log_line =~ /$re/; my ($date, @info) = @+{qw{date ACTION FAN_A FAN_B SEND FAILURE}}; # do something with $date and @info here }

See perlre - Extended Patterns for details of Named Capture Groups: (?<NAME>pattern)

-- Ken

Comment on Re: Some assistance with splitting variables
Select or Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2015-08-28 00:22 GMT
Find Nodes?
    Voting Booth?

    The oldest computer book still on my shelves (or on my digital media) is ...

    Results (334 votes), past polls