Reading last 3 lines from the file 
Author Message
 Reading last 3 lines from the file

Hi,

I want to read the last 3 or 2 lines of a text file. How do i do that?
I'm able to read the last line in the following way...

open (FH, "$file") || die "Cant read : $!\n";
while (<FH>)
{
  $last_line = <FH> unless eof(FH);

Quote:
}

close FH;
print "Last line: $last_line\n";

How to read last 3 lines? Pls help.

Thanks,
- Venu



Sun, 07 Nov 2004 16:39:57 GMT  
 Reading last 3 lines from the file

Quote:

> Hi,

> I want to read the last 3 or 2 lines of a text file. How do i do that?
> I'm able to read the last line in the following way...

> open (FH, "$file") || die "Cant read : $!\n";
> while (<FH>)

If you are on unix/linux the tail command can do it for you:

open (FH, "tail -3 $file|")

and the Perl script will only get the last three lines, no further
checks needed.

HTH,

        Marco



Sun, 07 Nov 2004 16:50:11 GMT  
 Reading last 3 lines from the file

Quote:

> I want to read the last 3 or 2 lines of a text file. How do i do that?

use File::ReadBackwards

http://search.cpan.org/search?mode=module&query=backwards

--

The Perl Review - a new magazine devoted to Perl
<http://www.theperlreview.com>



Sun, 07 Nov 2004 17:04:43 GMT  
 Reading last 3 lines from the file

  >> I want to read the last 3 or 2 lines of a text file. How do i do that?
  >> I'm able to read the last line in the following way...
  >>
  >> open (FH, "$file") || die "Cant read : $!\n";
  >> while (<FH>)

  MF> If you are on unix/linux the tail command can do it for you:

  MF> open (FH, "tail -3 $file|")

  MF> and the Perl script will only get the last three lines, no further
  MF> checks needed.

why fork off a process?

File::ReadBackwards is meant just for this problem.

uri

--

--- Boston Perl Classes ---- July 1-3 ---- http://stemsystems.com/class/ ----
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Sun, 07 Nov 2004 16:54:22 GMT  
 Reading last 3 lines from the file
"Venu" wrote ...

Quote:

> I want to read the last 3 or 2 lines of a text file. How do i do that?
> I'm able to read the last line in the following way...

> open (FH, "$file") || die "Cant read : $!\n";


print "-2: $lines[$#lines-2]\n-1: $lines[$#lines-1]\n 0: $lines[$#lines]\n";

Which assumes the file has at least 3 lines, is small enough to fit within memory, etc.

HTH,
BD



Sun, 07 Nov 2004 17:28:16 GMT  
 Reading last 3 lines from the file

Quote:

> Hi,

> I want to read the last 3 or 2 lines of a text file. How do i do that?
> I'm able to read the last line in the following way...

open (FH, "$file") or die "Can't read: $!\n";

print "$array[0]\n";
print "$array[1]\n";

close(FH);



Sun, 07 Nov 2004 18:07:02 GMT  
 Reading last 3 lines from the file

(snipped)

Quote:
> I want to read the last 3 or 2 lines of a text file. How do i do that?

This is a beginner's task requiring no module.

A presumption is made your "text file" exhibits
a standard newline delimiter for each unique line.

Due to your lack of clarity, my   $lines_to_read   is
used to represent your unclear amount of lines to read.

Work towards developing an ability to write clear,
concise and coherent articles which contain precise
parameters with which a reader may work.

Use of   tail   would be a better option for large files.

Godzilla!
--
Roberta The Remarkable Robot, {*filter*} Of Mind Munching!
  http://www.*-*-*.com/ ~{*filter*}/roberta/roberta.cgi
Chahta Chat, The Most Entertaining Chat To Be Found!
  http://www.*-*-*.com/ ~{*filter*}/webchat/chahta.cgi

#!perl

print "Content-type: text/plain\n\n";

$lines_to_read = 3;

if ($lines_to_read < 1)
 { print "Boss, you are a blithering idiot."; exit; }

open (TEST, "test.txt");

while (<TEST>)
 { $total_lines = $.; }

seek (TEST, 0, 0);
$. = 0;

if ($total_lines <= $lines_to_read)
 {
  print "File Only Contains $total_lines Lines:\n\n";
  while (<TEST>)
   { print $_; }
  close (TEST);
 }
else
 {
  while (<TEST>)
   {
    if ($. > ($total_lines - $lines_to_read))
     { print $_; }
   }
  close (TEST);
 }

exit;

CONTENTS OF TEST.TXT:
_____________________
line one
line two
line three
line four

PRINTED RESULTS:
________________
line two
line three
line four



Sun, 07 Nov 2004 19:11:43 GMT  
 Reading last 3 lines from the file

  G> print "Content-type: text/plain\n\n";

as always, this is not a web problem but moronzilla can't tell.

  G> $lines_to_read = 3;

  G> if ($lines_to_read < 1)
  G>  { print "Boss, you are a blithering idiot."; exit; }

typical moron code. hardwired value is tested against a constant.

  G> while (<TEST>)
  G>  { $total_lines = $.; }

gee, that is DUMB!! you only need to do the assignment at the end of the
loop.

  G> seek (TEST, 0, 0);
  G> $. = 0;

$. is not settable. it is read only.

  G> if ($total_lines <= $lines_to_read)
  G>  {
  G>   while (<TEST>)
  G>    {
  G>     if ($. > ($total_lines - $lines_to_read))
  G>      { print $_; }

and reading the entire file twice is a major luser thing to do.

so we have slow, crappy, bad, inflexible stupid code from moronzilla,
vs. a fast, stable, clear use of a module. maybe someone would even dare
to benchmark the two on a nice long file. i won't even bother as it is
too obvious which will win.

uri

--

--- Boston Perl Classes ---- July 1-3 ---- http://stemsystems.com/class/ ----
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Sun, 07 Nov 2004 19:35:12 GMT  
 Reading last 3 lines from the file
Uri Guttman, The CLPM Troll, suffering a bruised fragile masculine ego, spewed:

(snipped)

Your fragile masculine ego certainly makes a fool of you, frequently.
Again, you should strap a muzzle upon your unhealthy ego, Frank.

Godzilla!
--
Roberta The Remarkable Robot, {*filter*} Of Mind Munching!
  http://www.*-*-*.com/ ~{*filter*}/roberta/roberta.cgi
Chahta Chat, The Most Entertaining Chat To Be Found!
  http://www.*-*-*.com/ ~{*filter*}/webchat/chahta.cgi



Sun, 07 Nov 2004 19:58:57 GMT  
 Reading last 3 lines from the file

  G> Your fragile masculine ego certainly makes a fool of you, frequently.
  G> Again, you should strap a muzzle upon your unhealthy ego, Frank.

hmm, do i detect the anti-frank moron refusing to benchmark its crappy
code again?

uri

--

--- Boston Perl Classes ---- July 1-3 ---- http://stemsystems.com/class/ ----
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Sun, 07 Nov 2004 20:24:15 GMT  
 Reading last 3 lines from the file

Quote:


> > Hi,

> > I want to read the last 3 or 2 lines of a text file. How do i do that?
> > I'm able to read the last line in the following way...

> open (FH, "$file") or die "Can't read: $!\n";


> print "$array[0]\n";
> print "$array[1]\n";

> close(FH);

I like writing short code. So, here is my version.

open (FH, "$file") or die "Can't read: $!\n";


close(FH);

Prakash



Sun, 07 Nov 2004 20:16:36 GMT  
 Reading last 3 lines from the file

  JR> I like writing short code. So, here is my version.

  JR> open (FH, "$file") or die "Can't read: $!\n";

and slurping when not needed is slow.


use an array slice, not splice.

uri

--

--- Boston Perl Classes ---- July 1-3 ---- http://stemsystems.com/class/ ----
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Sun, 07 Nov 2004 20:39:58 GMT  
 Reading last 3 lines from the file
Do you need a completely perl way of doing it?
or can you use this?


Quote:



>> > Hi,

>> > I want to read the last 3 or 2 lines of a text file. How do i do
>> > that? I'm able to read the last line in the following way...

>> open (FH, "$file") or die "Can't read: $!\n";


>> print "$array[0]\n";
>> print "$array[1]\n";

>> close(FH);

> I like writing short code. So, here is my version.



> close(FH);

> Prakash



Sun, 07 Nov 2004 20:45:57 GMT  
 Reading last 3 lines from the file

Quote:

> I like writing short code. So, here is my version.

> open (FH, "$file") or die "Can't read: $!\n";


> close(FH);

You call that short?  :-)

print((<FH>)[-3..-1]);

John
--
use Perl;
program
fulfillment



Sun, 07 Nov 2004 20:47:43 GMT  
 Reading last 3 lines from the file

  j> Do you need a completely perl way of doing it?
  j> or can you use this?



how is that completely perl? calling external programs is not pure perl.

uri

--

--- Boston Perl Classes ---- July 1-3 ---- http://stemsystems.com/class/ ----
----- Stem and Perl Development, Systems Architecture, Design and Coding ----
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org



Sun, 07 Nov 2004 21:08:00 GMT  
 
 [ 24 post ]  Go to page: [1] [2]

 Relevant Pages 

1. read last line without reading previous lines, how?

2. Reading Last X Lines of File?

3. Reading the last line of a file

4. Quickly read the last 10 lines of a very large logfile

5. read last line only...

6. best way to read last line?

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

8. How to get last line in ASCII file?

9. trim the last blank-line and compare files

10. killingthe last line of the file

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

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

 

 
Powered by phpBB® Forum Software