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

Re: Reading Lines from Text file

by malduarte (Initiate)
on Feb 02, 2006 at 17:44 UTC ( [id://527390]=note: print w/replies, xml ) Need Help??


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/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-23 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found