How to get last line in ASCII file? 
Author Message
 How to get last line in ASCII file?

Hi,

Can anybody suggest a fast way to get the last line of a file?

best regards,

PeterM



Tue, 16 Dec 1997 03:00:00 GMT  
 How to get last line in ASCII file?

: Hi,

: Can anybody suggest a fast way to get the last line of a file?

$lastline = `tail -1 $file`;            #inefficient for small files


$lastline = $lines[ $#lines ];

--

Rick Poleshuck  | Voice - (908) 245-1177
                | Mail  - Rick Poleshuck & Associates
                |       - 308 Elizabeth Avenue, Cranford, New Jersey  07016



Wed, 17 Dec 1997 03:00:00 GMT  
 How to get last line in ASCII file?

: Can anybody suggest a fast way to get the last line of a file?

Two things come to mind,

On a small file one could:


$last_line = $array[$#array];
close(FP);

On large file this may be horribly inefficient.

On Unix one could:

open(FP,"tail -1 $filename|");
$last_line = <FP>;
close(FP);

Hope this helps,
Kurt



Wed, 17 Dec 1997 03:00:00 GMT  
 How to get last line in ASCII file?

: Hi,

: Can anybody suggest a fast way to get the last line of a file?

Try this:

while (<>)
{
        print if (eof);

Quote:
}

You can modify this depending on what you need to do with it.

Beirne

--
Beirne Konarski         | Subscribe to the Unicycling Mailing List

Kent State University   | Unicycling Web Page:
"Untouched by Scandal"        | http://nimitz.mcs.kent.edu/~bkonarsk/



Wed, 17 Dec 1997 03:00:00 GMT  
 How to get last line in ASCII file?
[e-mailed and posted]



Quote:

>Hi,

>Can anybody suggest a fast way to get the last line of a file?

>best regards,

>PeterM

$last_line = `tail -1 file_name`;

Hope this helps,
Jim



Wed, 17 Dec 1997 03:00:00 GMT  
 How to get last line in ASCII file?
|>
|>
|> Hi,
|>
|> Can anybody suggest a fast way to get the last line of a file?
|>
|> best regards,
|>
|> PeterM
|>

tail -1 <filename>
--
Richard Baumann                         (805) 447-8391
International Clinical Safety
AMGEN
---------------------------------------------------------------
The opinions expressed herein are strictly those of the author.



Wed, 17 Dec 1997 03:00:00 GMT  
 How to get last line in ASCII file?
Please note that comp.lang.perl is being replaced by comp.lang.perl.*,
followups redirected accordingly.



Quote:
>Can anybody suggest a fast way to get the last line of a file?

You'd have a hard time beating

$line = `tail -1 $filename` ;

without actually rewriting tail's algorithm in perl.

-Matt



Thu, 18 Dec 1997 03:00:00 GMT  
 How to get last line in ASCII file?

Quote:

>: Can anybody suggest a fast way to get the last line of a file?
>Try this:
>while (<>)
>{
>    print if (eof);
>}
>You can modify this depending on what you need to do with it.

I've tried it.  I also tried Randal's method.

On my test data (/etc/termcap), Randal's method is four times faster.

Just in case, here it is again:

        require 5.000;
        open FILE, $filename or die "can't read $filename: $!\n";
        seek FILE, -32768, 2 if -s FILE > 32768;
        print +(<FILE>)[-1];
        close FILE;

--
Hope this helps,

HansM



Sat, 20 Dec 1997 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. How to get last line in ASCII file?

2. Getting info of file last update

3. Even without binmode, txt file gets uploaded as BINARY instead of ASCII

4. better way of getting the last modified file?

5. WinNT: getting File Last Modified data from outside current directory

6. [question: cannot delete the last line of a file]

7. Reading Last X Lines of File?

8. trim the last blank-line and compare files

9. killingthe last line of the file

10. More Efficient Way To Get Last Line From A TXT File

11. Reading last 3 lines from the file

12. [Fwd: How can I get the last element on each line in a text file]

 

 
Powered by phpBB® Forum Software