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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.