how can i read only the last line of a files
Anonymous Monk
created: 2006-08-03 05:45:10
Hi monks,
I am trying to read 3 files at a time,below is my code.
#!/usr/bin/perl
open(F1,"tax.pl");
while($line=)
{
	chomp $line;
	@arr=split(/"\n"/,$line);
	 print "@arr\n";
}
open(F2,"accno.pl");
{
while($line1=)
	{
	chomp $line;
	@arr1=split(/"\n"/,$line1);
	print "@arr1\n";
	}
}
 open(F3,"ptax.pl");
 while($line=)
 {
	 chomp $line;
	 @arr3=split(/"\n"/,$line);
	 print "@arr3\n";
 }

  

The above code is reading all the contents of 3 files, i required to read only the last lines from these file, how can we read only the last lines from these files.
keep pouring your ideas.
Re: how can i read only the last line of a files
created: 2006-08-03 05:53:59

Try looking at either File::ReadBackwards or Tie::File.

--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Re^2: how can i read only the last line of a files
created: 2006-08-03 12:13:08
Hi, If you are trying to read last line of continously updated files (like weblog file), I think your best choice would be File::Tail inconjuction with Proc::Daemon. Thanks, Uday.
Re: how can i read only the last line of a files
created: 2006-08-03 06:03:37
hi monk,
i saw your code,u are reading the content of files in an array.below subroutine may helps to read the last content of arrays.
@last=lastlines(\@a,\@b,\@c);
print "@last\n";
sub lastlines
{
	my @retlist=();
     foreach my $aref(@_)
	{
		 push @retlist,pop @$aref;
	}
	return @retlist;
}
.
Re^2: how can i read only the last line of a files
created: 2006-08-03 07:57:14
This is equivalent code (though note [davorg]'s [id://565413|reply] for a much more robust solution) ... no need to iterate through everything:
sub lastlines
{
  return map { $_[-1] } @_;
}

# or, w/o using a sub:
@last = ($a[-1], $b[-1], $c[-1]);
Re: how can i read only the last line of a files
created: 2006-08-03 06:10:53

Is this a one time thing, or are the files updated from time to time and you need to retreive material added to the end?

Is there a maximum line length?

Are the files vary large?

Is each file the same length?


DWIM is Perl's answer to Gödel
Re: how can i read only the last line of a files
created: 2006-08-03 06:24:54
I think your best bet is to give GrandFather enough information to do something clever, elegant, and efficient. Pending that, here's my contributuion to getting the last line from each of the three files in a fairly basic way.
my $last_line;
for my $fname ( 'tax.pl', 'accno.pl', 'ptax.pl' ) {
  open INFILE, $fname;
  while () {
    chomp;
    $last_line = $_;
  }
  close INFILE;
  print "$fname:$last_line\n";
  $last_line = '';   # in case 2nd or 3rd file is empty
}
This will work with huge files, where you don't want to store the whole file in memory, although, by the extensions, I'm guessing your files are fairly short. Also, for huge files, you almost certainly want [mod://File::ReadBackwards] as davorg has already suggested.
Re: how can i read only the last line of a files
created: 2006-08-03 07:37:01
Here's an alternative way to do it.

The function last_n_lines takes 3 arguments:  the filename, the number of lines N to read from the end, and an optional maximum line length.  It then returns a reference to an array containing the last N lines from the file.  It will work especially well on very huge files, as it doesn't read all of the lines into memory first:

#!/usr/bin/perl -w
                                                                                
# Strict
use strict;
use warnings;
                                                                                
# User-defined
my $n    = 3;                   # How many lines to read from the end
my $file = "/usr/dict/words";   # File to read
                                                                                
                                                                                
# Main program
my $plines = last_n_lines($file, $n, 256);
print "Last $n line(s) from '$file':\n";
for (my $i = 0; $i < @$plines; $i++) {
    printf " %2d.  '%s'\n", $i+1, $plines->[$i];
}
                                                                                
                                                                                
#
#  In:  $1 ... name of file
#       $2 ... number of lines to read
#       $3 ... (optional) maximum line length
#
# Out:  $1 ... pointer to list of the last N lines from the file.
#
sub last_n_lines {
    my ($fname, $nlines, $maxlen) = @_;
                                                                                
    # Open the file
    open(my $fh, "<", $file) or die "Error -- failed to read '$fname' ($!)\n";
                                                                                
    # Calculate offset near end of file
    $maxlen ||= 1024;
    my $offset = (-s $file) - ($maxlen + 1) * $nlines;
    ($offset > 0) and seek($fh, $offset, 0);
                                                                                
    # Read lines
    my @lines;
    while (!eof($fh)) {
        chomp(my $line = <$fh>);
        push @lines, $line;
    }
                                                                                
    # Return last N
    close $fh;
    splice(@lines, 0, -$nlines);
    return \@lines;
}

Update:  Removed a line which I just realized I had put in during testing of the program.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: how can i read only the last line of a files
created: 2006-08-03 17:32:32
In addition - see [doc://eof]
eof # this is the last line
 and print while <$fh>


perlmonks.org content © perlmonks.org and Anonymous Monk, davidrw, davorg, GrandFather, liverpole, perladdict, rodion, sh1tn

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03