Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Note that ^A represents the SOH character (Ascii val 1).2009/01/15 01:23:45:678: ASDF: [8=FIX.4.4^A9=228^A35=D^A49=ZYXW^A56=MY +CO^A34=6^A52=20090115-01:23:45^A116=BLAH^A129=HALB^A50=MEH^A1=HEM^A11 +=abcefg123456^A15=ZZZ^A21=1^A22=5^A38=100^A40=2^A44=4.80000000^A48=ZV +ZZT.N^A54=2^A55=ZVZZT^A59=0^A60=20090115-01:23:45^A100=MEH^A10=111^A]
My goal is to be able to capture the value of any given tag. So far, I have tried this:
My output is a pipe delimited set of values. The above PERL works with the exception that each value in my output contains the "^A". I want to correct his by capturing just the value between "\d\d\d\d=" and the next "^A" (ungreedy) where every "d" is known. If no match is found (i.e. this tag is not present) I want to output just a "blank" pipe.if($line =~ m/^A55=(.*?^A)/){ print "$1|"; } else { print "|"; }
The second issue I would like to resolve is to simplify or generalize my statements. Currently I have a series of if statements, such as the one above, checking for every tag I'm trying to capture (e.g. 55=, 48=, 22=). I'd like to see if there is a more "clever" way to do this in a single statement. Something such as this, perhaps:
Please note that if one of the patterns above does not match, I'd like the corresponding $buffer variable to contain a blank rather than the next matched group value.if($line =~ m/(^A22=.*?^A).*(^A40=.*?^A).*(^A48=.*?^A).*(^A54=.*?^A).* +(^A55=.*?^A)/g){ print "$1|$2|$3|$4|$5\n"; }
Thanks very much for your time and consideration,
_j
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Log Parsing using Regex
by moritz (Cardinal) on Jul 09, 2009 at 12:55 UTC | |
by Anonymous Monk on Jul 09, 2009 at 13:25 UTC | |
by AnomalousMonk (Archbishop) on Jul 09, 2009 at 15:33 UTC | |
|
Re: Log Parsing using Regex
by jethro (Monsignor) on Jul 09, 2009 at 13:22 UTC | |
|
Re: Log Parsing using Regex
by johngg (Canon) on Jul 09, 2009 at 22:55 UTC | |
by Anonymous Monk on Jul 21, 2009 at 12:24 UTC |