Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Search file for certain lines

by kcott (Abbot)
on Sep 24, 2013 at 05:33 UTC ( #1055439=note: print w/ replies, xml ) Need Help??


in reply to Search file for certain lines

G'day Jalcock501,

You can read your input as multiline blocks: see '$/' in perlvar. Each of the individual lines in those blocks can be matched for the starting characters you want: see the 'g' and 'm' modifiers, the '^' and '$' anchors and '[...]' character classes in perlre.

With the sample input you provided, this code:

#!/usr/bin/env perl -l use strict; use warnings; use autodie; my $re = qr{^([jEG].*)$}m; { local $/ = "\nh"; open my $fh, '<', 'pm_1055252_data.txt'; while (<$fh>) { print "*** h-block #$."; print $1 while /$re/g; } close $fh; }

produces this output:

*** h-block #1 j1000010017 6790194100109201301092013Test Data N PW09-3PY248 +018BIK20 E99HEADER|004|001| E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA01_1||||||"LNI10708"| G3301R7435459:LNI10708 *** h-block #2 j1000010017 6790194100109201301092013Test Data M PW09-3PY248 +005BIK00 E99HEADER|004|001| E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA01_1||||||"LNI10708"| G3301R7435459:LNI10708 *** h-block #3 j1000010017 6790194100109201301092013Test Data L PW09-3PY248 +006BIK10 E99HEADER|004|001| E99INSSCH|248| E99POLCOM|3||CAP01|66|3301R7435459||||| E99INSFAC2|MSRA01_1||||||"LNI10708"| G3301R7435459:LNI10708

-- Ken


Comment on Re: Search file for certain lines
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2015-07-31 06:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls