Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Delete every n line

by mr_mischief (Monsignor)
on May 09, 2008 at 15:05 UTC ( [id://685694]=note: print w/replies, xml ) Need Help??


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.:)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://685694]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found