in reply to Reading Lines from Text file

This is a good example of a simple question which can have complex answers depending of the circumstances....
  1. Small files:
    perl -e "@l = <>; print 'first : ', $l[0], 'last : ', $l[$#l];" filen +ame
  2. 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)

  3. 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.


  4. 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 :-)


  5. There's More Than Way To Do It,

Replies are listed 'Best First'.
Re^2: Reading Lines from Text file
by holli (Abbot) on Feb 02, 2006 at 19:48 UTC
    • 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.
    Or use File::ReadBackwards as ikegami suggests.


    holli, /regexed monk/