Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
To print the first and last line of each file:
$/ = undef; while (<>) { print "\n$ARGV\n", / (.+?) ^.*^ (.+?) \z/xms }
To find the first and last match for a pattern:
(In this case, 'DT=20011105.112009'-style time-stamps in a log file)
$dtrx = qr/[Dd][Tt]=(\d{8}\.\d{6})/; $/ = undef; while (<>) { ($aa, $z) = /$dtrx # get the first one (?: # and maybe, .* # after maybe many lines, ^.*? $dtrx # the first one on the last line )? # that has one /mxs; $aa or next; $z ||= ''; # or '' if no more print "$ARGV\t $aa -- $z\n"; }
In this case there can be more than one such pattern on a line and it's the first one that is valid.

The point being that the /s makes a greedy, black-hearted .* for going straight to the end of the (presumedly long) file, ignoring linebreaks.   Then the /m (together with the ? to reign in dot-star) allows backtracking by line to find the first match on a line with matches.  

update:   These started out (obviously) as one liners, then got thrown into files.  The first time I actually looked at them was when I grabbed them to throw up here which, of course, lead to exploring MTOWTDI.  The first seems clearest this way. The second started out being far more complicated which is why it's in multi-line /x format.

  p

In reply to Re: Call for code samples! by petral
in thread Call for code samples! by japhy

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 romping around the Monastery: (5)
As of 2024-04-18 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found