There has got to be a better way to do the following than this:
use strict;
use warnings;
sub Last_N_Lines
{
my($String, $N) = @_;
my @Lines = split(/\n/, $String);
if(scalar(@Lines) > $N)
{
splice(@Lines, 0, scalar(@Lines) - $N);
}
return(join("\n", @Lines))
}
my $String = "This is line 1
This is Line 2
This is Line 3
This is Line 4
This is Line 5
This is Line 6
This is Line 7
This is Line 8
This is Line 9
This is Line 10";
print("Last\[5\] Lines.\[\n" . Last_N_Lines($tring, 5) . "\]\n");
This works fine and produces this output:
Last[5] Lines.[
This is Line 6
This is Line 7
This is Line 8
This is Line 9
This is Line 10]
I struggled with various regex ways of getting the same result, but it was beyond me. Google and Super Searches were not helpful either.