Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Reading file and matching lines

by Eily (Curate)
on Feb 11, 2014 at 16:07 UTC ( #1074433=note: print w/ replies, xml ) Need Help??


in reply to Reading file and matching lines

In the name of Tim Toady (There Is More Than One Way To Do It). Featuring the range, or flip-flop operator, which translates in human as "From .. till ..", and the next keyword.

my $count =0; LINE: while(<DATA>) { next LINE unless /^E/../^G/; # next line unless we are between a li +ne starting with a end a line starting with G die "Oups, went too far!" if /^h/; # error if the line starts with +an h and hasn't been skipped by the previous statment $count++ unless /^G/; # count that do not start with a G } __DATA__ Q165HWN0X001 Q165HWN0X002 Q165HWN0X003 E99HEADER|006|001 E99INSSCH|052| E99POLCOM|1||IIL|62|35119849249024||||| E99INSFAC2|C00124||||||XAJX0727,YGAX0000,ZAAJ0203,VABA0018,WJZA1800| G35119849249024 h189SMA2


Comment on Re: Reading file and matching lines
Download Code
Replies are listed 'Best First'.
Re^2: Reading file and matching lines
by tbone654 (Beadle) on Feb 11, 2014 at 20:26 UTC

    One liner

    Prompt> perl -ne ' next if !/^E/; print $_; ' datafile
    
    E99HEADER|006|001
    E99INSSCH|052|
    E99POLCOM|1||IIL|62|35119849249024|||||
    E99INSFAC2|C00124||||||XAJX0727,YGAX0000,ZAAJ0203,VABA0018,WJZA1800|
    

      modified one-liner and data to test...

      F98020A@LUS76E8012758 /cygdrive/c/package
      $ perl -ne ' if (/^h/){print "error starts with h program exiting"; exit;} if (/^G/){exit;} next if !/^E/; print $_;' data
      E99HEADER|006|001
      E99INSSCH|052|
      E99POLCOM|1||IIL|62|35119849249024|||||
      E99INSFAC2|C00124||||||XAJX0727,YGAX0000,ZAAJ0203,VABA0018,WJZA1800|
      
      F98020A@LUS76E8012758 /cygdrive/c/package
      $ cat data  (note: changed the data to have E's after old)
      Q165HWN0X001
      Q165HWN0X002
      Q165HWN0X003
      E99HEADER|006|001
      E99INSSCH|052|
      E99POLCOM|1||IIL|62|35119849249024|||||
      E99INSFAC2|C00124||||||XAJX0727,YGAX0000,ZAAJ0203,VABA0018,WJZA1800|
      G35119849249024
      h189SMA2
      E99INSSCH|052|
      E99POLCOM|1||IIL|62|35119849249024|||||
      E99INSFAC2|C00124||||||XAJX0727,YGAX0000,ZAAJ0203,VABA0018,WJZA1800|
      

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (15)
As of 2015-07-31 20:33 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 (281 votes), past polls