Skip to content

Commit

Permalink
Added usage, simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpost committed Dec 5, 2014
1 parent f1e2bc8 commit eec7944
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions mid
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#!/usr/bin/perl

# returns the specified line 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

# Usage: mid <lineno> [files]
# Usage: cat <file> | mid <lineno>
# 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);
exit if ($split and ! $num2);
die usage() unless $arg and (! $split or $num2);

my $i = $num1;
my $j = ($split)
? ( ($split eq "+") ? ($num1 + $num2) : ($num2) )
: $num1;

#print STDERR "$i,$split,$j\n";
? ( ($split eq "+") ? ($num1 + $num2) : ($num2) )
: $num1;

exit unless $i > 0;
exit usage() unless $i > 0;

if (@ARGV > 1) {
# if multiple files are given, recurse to calling mid on each one
Expand All @@ -28,6 +23,19 @@ if (@ARGV > 1) {
while (my $line = <>) {
next if $. < $i;
print $line;
last if $. >= $j;
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);
}

0 comments on commit eec7944

Please sign in to comment.