Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Delete line if REGEX == true

by DrAxeman (Scribe)
on Aug 07, 2005 at 02:10 UTC ( [id://481595]=perlquestion: print w/replies, xml ) Need Help??

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

I'm getting the hang of using Regular Expressions. Not how to write them, but how to use them in Perl. I have a challenge that I can't google anywhere.

I need to delete the lines of a file if a certain REGEX is true.

if ($. =~ /\,\,\,\,\,/) { -delete line- $. }

Replies are listed 'Best First'.
Re: Delete line if REGEX == true
by davidrw (Prior) on Aug 07, 2005 at 02:18 UTC
    note that $. is just a integer that is the current line number -- it's not the actual line .. depending on your context, $_ might be that.. What's the rest of your code? Some samples of some common situations:
    foreach my $line ( @lines ){ next if $line =~ /,{5}/; # do something } perl -ne ' do_some_print_statement_here unless /,{5}/' some_file.txt my @lines = ...; my @good_lines = grep( $_ !~ /,{5}/, @lines );

      Since Unix is much more than an environment to run perl under, I'll offer another solution which may make more sense if all you're using perl for is deleting lines based on a regex. No point in dragging in a good, general purpose tool over a good, specific purpose tool when that's the purpose you need it for. ;-)

      sed -e '/,,,,,/d' < source.txt > destination.txt
      There are a lot of good tools that come with unix. They can be worth the time to investigate - even if you don't use them, they can be invaluable guides in designing your own code to do something similar in perl.

        good point. And of course besides sed there's grep, which i think is even better here (read: teaching environment) because of the direct parallel to perl.
        grep -v ,,,,, source.txt > destination.txt
Re: Delete line if REGEX == true
by AReed (Pilgrim) on Aug 07, 2005 at 05:27 UTC
    However, if you insist on using perl... ;-)
    use strict; use warnings; while (<>) { print unless /,,,,,/; }

    ...will do it. Or the command-line equivalent:

    perl -ne "print unless /,,,,,/" infile > outfile

    Please note that I only tested this under Windows, so you may have to change double-quotes to single-quotes on a Unix system.

      The key observation here is that you're making a copy of the file without those lines in it. There's no straightforward way to delete the lines directly from the file; instead you have to think about it slightly backwards: print the lines you want to keep, and just skip the ones you want to delete. When you're done, you can rename the new file onto the original, or you can use perl's -i switch to take care of that for you.
        There is too. Add an -i.bak and forget the redirection. See perlrun for -i.

        C.

Re: Delete line if REGEX == true
by neniro (Priest) on Aug 07, 2005 at 09:35 UTC
    Another way to directly remove unwanted lines is using Tie::File and greping those lines you want:
    #!/usr/bin/perl use strict; use warnings; use Tie::File; my @text; tie @text, 'Tie::File', 'test_filter.txt' or die "Something went wrong: $!\n"; @text = grep { !/,,,,,/ } @text; untie @text; exit;
Re: Delete line if REGEX == true
by anonymized user 468275 (Curate) on Aug 08, 2005 at 14:28 UTC
    If the idea is to make it terse enough to compete with sed or grep, this is the tersest I have so far...
    print grep !/\,\,\,\,\,/, <>;
Re: Delete line if REGEX == true
by Anonymous Monk on Jul 08, 2008 at 12:46 UTC
    Its very easy to do. I just do in a find replace window that excepts regular expressions if "brother" is the string your looking for heres an example. Replace ^.*brother.*\n with nothing ( blank)
      that wont remove lines without the "brother string" so... 1 2 3 brother 3 2 1 4 5 6 sister 7 8 9 will reduce to brother 4 5 6 sister 7 8 9 which then becomes the problem as originally stated.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2024-04-23 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found