-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtailz
More file actions
executable file
·32 lines (23 loc) · 799 Bytes
/
tailz
File metadata and controls
executable file
·32 lines (23 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/perl
# Simple program to read the last n line(s) of a file.
# Reads from the end of the file for efficiency.
# Originally coded by zentara on 06 September 2002:
# https://www.perlmonks.org/index.pl?node_id=195768
# Improved by Trizen on 11 February 2012
# usage tailz filename numberoflines
my $filename = shift or die "usage: $0 file numlines\n";
my $numlines = shift // 10;
my $byte;
# Open the file in read mode
open my $fh, '<', $filename or die "Couldn't open $filename: $!";
# Rewind from the end of the file until count of eol 's
seek $fh, -1, 2; # get past last eol
my $count = 0;
while (tell($fh) > 0) {
seek $fh, -1, 1;
read $fh, $byte, 1;
last if $byte eq "\n" and ++$count == $numlines;
seek $fh, -1, 1;
}
local $/ = undef;
print scalar <$fh>;