Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Capturing the first and last line of a file ?

by Abigail-II (Bishop)
on May 06, 2003 at 12:27 UTC ( [id://255859]=note: print w/replies, xml ) Need Help??


in reply to Capturing the first and last line of a file ?

A simple way:
print scalar <FOO>; while (<FOO>) {print if eof}

or

$ perl -ne 'BEGIN {print scalar <>} print if eof' /path/to/your/fi +le

Or you could use the toolkit:

system head => -1 => $file; system tail => -1 => $file;

The latter solution always prints 2 lines, even if the file contains 1 line - the first two solutions will print just 1 line. If the input file is empty, the first two solutions will start reading from STDIN, while the latter won't print anything.

Abigail

Log In?
Username:
Password:

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

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

    No recent polls found