Skip to content

Instantly share code, notes, and snippets.

@jkg
Last active October 7, 2019 22:09
Show Gist options
  • Save jkg/5f94e8462cfc413eac0652a114b220df to your computer and use it in GitHub Desktop.
Save jkg/5f94e8462cfc413eac0652a114b220df to your computer and use it in GitHub Desktop.
#!perl
use strict; use warnings;
use Getopt::Std;
our ($opt_n, $opt_v);
getopts('vn:');
if ( $opt_v ) {
warn "warning you every $opt_n lines";
}
my $counter;
while (<>) {
$counter++;
unless ( $counter % $opt_n ) {
warn "Processed $counter lines";
}
print $_;
}
@OpossumPetya
Copy link

while (<>) {
    $counter++;
    warn "Processed $counter lines\n" unless $counter % $opt_n;
    print;
}

\n at the end of warn() text suppresses the "at line N ..." after each output
$_ at the end of print() can be omitted as print will implicitly use it.

or perl -ne 'BEGIN{$n=3} $c++; warn "Processed $c lines\n" unless $c % $n; print' filename.ext

-n -- assumes while(<>) code
-e -- oneliner
BEGIN {} block will be executed once before start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment