Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!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<STDIN>, for pipelining: tail -5000 bigfile.txt | per -100 # show every 100th line for the # last 5000 in the file =head1 DESCRIPTION C<per> writes every C<N>th line, starting with C<OFFSET>, to C<STDOUT>. =head1 OPTIONS =over =item -N the integer value C<N> provided (e.g. C<-50>, C<-2>) is used to decide which lines to return -- every C<N>th. =item -oOFFSET the value C<OFFSET> provided says how far down in the input to proceed before beginning. The output will begin at line number C<OFFSET>. Default is 1. =item [ files ] =back Note that C<per> works on files specified on the commandline, or on C<STDIN> if no files are provided. The special input file C<-> indicates that remaining data should be read from C<STDIN>. =cut __END__

In reply to per - selects every Nth line by jkahn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (3)
    As of 2020-12-01 04:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?