#!perl use strict; use warnings; use constant DEBUG => 0; my ($divisor,$offset) = handleArgs(); if (DEBUG) { warn "offset $offset\n"; warn "divisor $divisor\n"; } while (<>) { next if $. < $offset; # haven't reached the first offset next if (($. - $offset) % $divisor); print; } sub handleArgs { my ($offset, $divisor); while (@ARGV and $ARGV[0] =~ s/^-//) { my $arg = shift @ARGV; if ($arg =~ s/^o//) { if (defined $offset) { warn "-o switch found more than once\n" } $offset = $arg; } else { if ($arg eq '') { unshift @ARGV, '-'; last; # arg was '-', which says "ignore following" } if (defined $divisor) { warn "divisor argument (-N) found more than once\n"; } $divisor = $arg; } } if (not defined $divisor) { die "no divisor (-N) defined on commandline!\n"; } if (not defined $offset) { $offset = 1; } if ($divisor <= 0) { die "divisor $divisor is <= 0, which makes no sense.\n"; } if ($offset <= 0) { die "offset $offset is <=, which makes no sense.\n"; } if ($divisor != int($divisor)) { warn "divisor $divisor non-integer. truncating\n"; $divisor = int($divisor); } if ($offset != int($offset)) { warn "offset $offset non-integer. truncating\n"; $offset = int($offset); } return ($divisor, $offset); } =head1 NAME per - return one line per N lines =head1 SYNOPSIS per [-oOFFSET] -N [files] per -90 -o2 file.txt # every 90th line starting with line 2 per -o500 -3 file.txt # every 3rd line starting with line 500 per -o1 -2 file.txt # every other line, starting with the first per -2 file.txt # same as above It can also read from C, for pipelining: tail -5000 bigfile.txt | per -100 # show every 100th line for the # last 5000 in the file =head1 DESCRIPTION C writes every Cth line, starting with C, to C. =head1 OPTIONS =over =item -N the integer value C provided (e.g. C<-50>, C<-2>) is used to decide which lines to return -- every Cth. =item -oOFFSET the value C provided says how far down in the input to proceed before beginning. The output will begin at line number C. Default is 1. =item [ files ] =back Note that C works on files specified on the commandline, or on C if no files are provided. The special input file C<-> indicates that remaining data should be read from C. =cut __END__