Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read the last two lines, not the first two in Perl

Tags:

perl

I am trying to create a Perl script that outputs the last two lines of a file. My current script accomplishes what I want, except that it reads the first two lines rather than the last two.

use strict;
use warnings;

my $file = 'test.txt';
open my $info, $file or die "Could not open $file: $!";

while( my $line = <$info>)  {   
    print $line;    
    last if $. == 2;
}

close $info;

What must I alter so that the script reads the last two lines rather than the first two?

like image 584
Opc Avatar asked Dec 04 '22 05:12

Opc


1 Answers

This is harder than printing the first lines! Here are a few options, ranging from least to most clever:

Just use tail

Because who actually needs a perl script to print the last two lines of a file?

Read all the lines from the file into a list, and print the last two.

print do {
    open my $info, $file or die ...;
    (<$info>)[-2, -1];
};

Read all the lines from the file, only remembering the last two.

open my $info, $file or die ...;
my @lines;
while (my $line = <$info>) {
  shift @lines if @lines == 2;
  push @lines, $line;
}
print @lines;

Read the file backwards, looking for line-endings; when two are found, read forwards from that point.

This is the most memory efficient, especially if a "line" is worryingly long, but complicated. However, there's a module for it: File::ReadBackwards. It lets you do:

use File::ReadBackwards;
my $info = File::ReadBackwards->new($file) or die ...;
my $last = $info->readline;
my $second_last = $info->readline;
print $second_last, $last;
like image 179
hobbs Avatar answered Dec 27 '22 05:12

hobbs