in reply to Searching a string in running file
If
c:\@Work\Perl\monks>perl -wMstrict -le "my @lines = ( qq{TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rulefi +lecycle 0\n}, qq{TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rilefu +llcycle 0\n}, ); ;; my $d_num = qr{ \d+ }xms; my $h_num = qr{ [[:xdigit:]]+ }xms; ;; for my $line (@lines) { print qq{[[$line]]}; ;; my $match = my ($seed, $p1, $p2, $p3, $err, $pid, $rfc) = $line =~ +m{ \A TPSM \s+ seed \s+ ($h_num) \s+ pass \s+ ($d_num) \s+ x \s+ ($d_num) \s+ x \s+ ($d_num) \s+ errors \s+ ($d_num) \s+ pid \s+ ($d_num) \s+ rulefilecycle \s+ ($d_num) \s* \Z }xms; ;; if ($match) { print qq{line ok, seed $seed p1 $p1 p2 $p2 p3 $p3 err $err +pid $pid rfc $rfc \n}; } else { print qq{line malformed}; } } " [[TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rulefilecy +cle 0 ]] line ok, seed 234B8177 p1 1 p2 2 p3 50 err 0 pid 34562 rfc 0 [[TPSM seed 234B8177 pass 1 x 2 x 50 errors 0 pid 34562 rilefullcy +cle 0 ]] line malformed
I understand that this code is perhaps a bit advanced for you right now, but this is a learning experience. Please consider carefully the wise advice of haukex, and also see perlre and also some of the Pattern Matching, Regular Expressions, and Parsing tutorials in the PerlMonks Tutorials section.
Give a man a fish: <%-{-{-{-<
|
|---|