Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Delete every n line

by kana (Initiate)
on May 09, 2008 at 14:42 UTC ( #685686=perlquestion: print w/replies, xml ) Need Help??

kana has asked for the wisdom of the Perl Monks concerning the following question:

Hi everybody! I need to delete every 25th line in text file. How do I do this? Thanks in advance.

Replies are listed 'Best First'.
Re: Delete every n line
by Fletch (Bishop) on May 09, 2008 at 14:54 UTC
    • open the file
    • Read the lines of the file in a while loop
      • Figure out if this line is a multiple of 25 (hint $.)
      • If it isn't, print it
    • close the file
    • underpants Profit!

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Delete every n line
by citromatik (Curate) on May 09, 2008 at 14:55 UTC

    Using a one-liner:

    perl -i.bk -ne 'print if ($. % 25 != 0)' file

    Hope this helps

    citromatik

Re: Delete every n line
by apl (Monsignor) on May 09, 2008 at 14:56 UTC
    Show us what you've tried. Or ask your teacher.

    Failing that:

    • Open the exsting file and a new file.
    • For each line in the file
      • BLACK MAGIC! What do you think you should do?
    • Close both files.
Re: Delete every n line
by mr_mischief (Monsignor) on May 09, 2008 at 15:05 UTC
    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.

      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.:)
Re: Delete every n line
by b10m (Vicar) on May 09, 2008 at 14:50 UTC

    TIMTOWTDI: Sed is probably the fastest way here ;-)

    sed '0~25d' filename

    Others will probably show fance Perl golf examples (hopefully).

    --
    b10m
Re: Delete every n line
by ambrus (Abbot) on May 09, 2008 at 19:22 UTC

    Others have given solutions with extensions in gnu sed or tricky perl commands. There's however a very easy way to solve this with the unix toolkit that should work everywhere. No need for perl or awk or sed or anything. Suppose the input file is named inp. Then run the following shell commands.

    ( cat inp; echo ) > gre rm spl.* split -l 25 gre spl. head -qn-1 spl.* > out rm gre spl.*
    The output is in the file out.

    Update 2010-03-07: See Re^2: Joining two files on common field for a list of other nodes where unix textutils is suggested.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2022-06-29 18:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (97 votes). Check out past polls.

    Notices?