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

Re: Some assistance with splitting variables

by kcott (Chancellor)
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027253]
[marto]: good morning all
[karlgoethebier]: hi marto
[karlgoethebier]: hi all

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-12-13 12:24 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (362 votes). Check out past polls.