http://www.perlmonks.org?node_id=1055439


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