⭐ in reply to How do I copy out the last line written in a file?
Read the entire file
This involves reading the entire file before examining the last line, which can be costly in memory and performance for large files.@array = <FILE>; $last_line = $array[$#array];
Seek to the end and backtrack
Use File::ReadBackwardsseek(FILE, -81, 2); # 81 bytes from the end, which should +be enough @lines = <FILE>; $last_line = $lines[$#lines]; # similar to above, but we don't read +much
This latter method is supposed to be efficient and fast, so I imagine it uses some variation of the seek() method above.tie *FILE, File::ReadBackwards 'input_file'; while (<FILE>) { # line-by-line starting from the end }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Answer: How do I copy out the last line written in a file?
by Anonymous Monk on May 03, 2007 at 01:56 UTC | |
by blazar (Canon) on May 03, 2007 at 10:18 UTC |