in reply to Delete every n line

b10m has a good solution based on a common Unix tool (that's available for other platforms, but not as common on them), but how about a Perl solution?

Here's a one-liner you can use from the command line:

perl -ne 'next if 0 == $. % 25; print' filename

Here's a somewhat clearer version:

#!/usr/local/bin/perl -- use strict; use warnings; my $remove_lines_modulo = 25; while ( <> ) { next if 0 == $. % $remove_lines_modulo; print; }

Or one that accepts an argument for which lines to remove:

#!/usr/local/bin/perl -- use strict; use warnings; my $remove_lines_modulo = shift @ARGV; die "Cannot handle zero as an argument.\n" if 0 == $remove_lines_modul +o; while ( <> ) { next if 0 == $. % $remove_lines_modulo; print; }

Please remember that if this is a homework assignment, it's proper form to say so. Asking for completed work instead of help when it's a school assignment doesn't do any good in the long run and isn't fair to students who actually did the work.

Replies are listed 'Best First'.
Re^2: Delete every n line
by kana (Initiate) on May 19, 2008 at 07:49 UTC
    thank you for your help! no it isn't homework, it is just part of some larger scripts for which it took me long time to program, bc I am just a starter in perl. This way I can save some precious time.:)