forked from mjpost/bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmid
executable file
·46 lines (39 loc) · 1.08 KB
/
mid
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/perl -C31
# Returns the requested line number from a file or list of files.
# If the line number is given as i:j or i-j, selects that range.
# If no file is given, we read from STDIN.
my $arg = shift;
($num1,$split,$num2) = split(/([:\-\+])/,$arg);
die usage() unless $arg and (! $split or $num2);
my $i = $num1;
my $j = ($split)
? ( ($split eq "+") ? ($num1 + $num2) : ($num2) )
: $num1;
exit usage() unless $i > 0;
if (@ARGV > 1) {
# if multiple files are given, recurse to calling mid on each one
foreach my $file (@ARGV) {
if ($file =~ /.gz$/) {
system("zcat -f $file | $0 $i");
} else {
system("cat $file | $0 $i");
}
}
} else {
# else, just print the ith line
while (my $line = <>) {
next if $. < $i;
print $line;
last if $. >= $j;
}
}
sub usage() {
print STDERR <<EOF;
Prints the Nth line of a file or of each of a list of files.
Usage:
mid LINENO FILE1 [FILE2 FILE3 ...]
cat FILE | mid LINENO
where LINENO is the line number of the file (starting with 1).
EOF
exit(1);
}