This is a good example of a simple question which can have complex answers depending of the circumstances....
- Small files:
perl -e "@l = <>; print 'first : ', $l[0], 'last : ', $l[$#l];" filen
+ame
This approach is only recommended for small files because all the lines are being read to an array (copying everything to memory before printing the lines)
- Medium sized files:
perl -e "$first = <>; while($line = <>) { $last = $line; }; print 'fir
+st : ', $first, 'last : ', $last;" filename
This approach is recommended for medium sized files because it's reading one line at a time into memory.
It's still not recommended for larges files because the entire file is being read. If you have a 4gbyte file and your I/O subsystem is only able to read 10 Mbytes/second you're going to take 400 seconds to get the answer.
- Really large files
For really large files you can read the first line normally (see above). To read the last line, you have to:
- seek to the end of the file
- search for the line terminator of the line before the last
- read from the position of the character after the line terminator to the end of file.
Update: Use File::ReadBackwards . I didn't knew about this module and ended up reinventing the wheel (and ended up with a square wheel when compared with File::ReadBackwards :-)
There's More Than Way To Do It,