Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howe to read the last line in the file? 1

Status
Not open for further replies.

vasilek

Programmer
Jan 5, 2003
99
US
I need to see what is the value of a "field" on the last line in the text file.

I know how to open the file, but do not know how to read only what is on the last line?

I started like this:

open (FILEHANDLE, &quot;<../data/textfile.file&quot;) || print (&quot;Cannot open file&quot;);

Thanks.
 
how about this - as long as on UNIX...

$lastLine = `tail -1 ../data/textfile.file`;

print &quot;$lastLine\n&quot;;


Regards
Duncan
 
if you have a relatively small file compared to the free memory, the you can use something like this

Code:
open (FILEHANDLE, &quot;<../data/textfile.file&quot;) || print (&quot;Cannot open file&quot;);
 
my @lines = <FILEHANDLE>;
my $line = pop @lines;

---
cheers!
san
pipe.gif


&quot;The universe has been expanding, and Perl's kind of been expanding along with the universe&quot; - Larry Wall
 
Here's a quick and dirty way to do it:
Code:
open (FILEHANDLE, &quot;< ../data/textfile.file&quot;) || print (&quot;Cannot open file&quot;); 
while (<FILEHANDLE>) { $lastline = $_; }
After the while loop $lastline will have the last line in the file.

Hope taht helps.
 
Thanks, icrf. I had considered a solution that uses lowlevel io, but, hey, someone else already invented this wheel so who am I to recreate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top