diff --git a/mid b/mid index 73d88b3..0e4d7a8 100755 --- a/mid +++ b/mid @@ -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 [files] -# Usage: cat | mid +# 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 @@ -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 <