Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I can, off the top of my head, think of at least three ways, which I view as distinct:

  • Use Tie::File, which lets you treat a file (more or less) as an array.
  • Use File::ReadBackwards, which (duh!) reads a file backwards.
  • Read the file a record at a time, but keep track of the contents of the current and previous record, and print them as needed.

If your data are as shown (a name, followed by lines starting with labels, such as <id:>, <city:>, etc), something like this may work:

#!perl use strict; use warnings; open($my fh, "<", $infile) or die "Could not open $infile because $!\n +"; while(<$fh>){ next if /^<id:>|^[A-Za-z]/; print; }

Now, if my regex brain is turned on, this regex should skip lines which start with <id:> or start with letters. Incidently, how is this anonymyzing data if you're leaving addresses and phone numbers?


Update

Having noticed ww's comment in a message, I may have misread or confused the title ("Remove line above matching criteria") and "extract name and id". The regex I put in the sample above (unless I screwed it up) should skip the name and id; to skip everything else one could change the "if" to "unless".

emc

If it's not foggy out, I need new glasses.


In reply to Re: Remove line above matching criteria by swampyankee
in thread Remove line above matching criteria by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found