#!/usr/bin/perl -w # # torso # # prints the specified lines from the input # # Seth Golub # http://www.aigeek.com/geek/#torso # # 16 Aug 2000 # # This script is released to the public domain. use strict; use English; use Getopt::Std; my %opts; getopts( "hs:e:n:m", \%opts ) && !defined($opts{h}) or usage(); my $start = 1; my ( $end, $num ); $start = $opts{s} if defined( $opts{s} ); if ( defined($opts{s}) && $opts{s} !~ /^\d+$/ ) { die "-s argument must be a positive integer, not \"$opts{s}\".\n"; } if ( defined($opts{n}) ) { if ( $opts{n} !~ /^\d+$/ ) { die "-n argument must be a positive integer, not \"$opts{n}\".\n"; } $num = $opts{n} ; $end = $start + $num - 1; } if ( defined( $opts{e} ) ) { if ( $opts{e} !~ /^\d+$/ ) { die "-e argument must be a positive integer, not \"$opts{e}\".\n"; } if ( defined($end) ) { $end = ($end < $opts{e}) ? $end : $opts{e}; # MIN } else { $end = $opts{e} } } if ( defined($end) ) { if ($end < $start) { exit; } } while( <> ) { next if $INPUT_LINE_NUMBER < $start; print; if ( defined($end) && ($INPUT_LINE_NUMBER >= $end) ) { last if !defined($opts{m}); close( ARGV ); next; } } continue { # reset line close( ARGV ) if $opts{m} && eof; } exit; sub usage { $0 =~ s@.*/@@; die "Usage: $0 [-s start][-n lines][-e end][-m] file1 file2 .. -m Treat each input file separately, rather than concatenating. -s # Start at line <#>, defaults to 1. -n # Print <#> lines from -e # Print through line <#> If neither -n nor -e is given, the rest of the file will be printed. If both -n and -e are given, the fewer lines specfied will be printed. "; }