Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

delete a line by a string

by moked (Beadle)
on Apr 02, 2003 at 13:55 UTC ( [id://247489]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All I wish to search a file, and every time I find a certain string (exact match not as a part of a word). I wish to delete the whole line. If anyone can help me with a code. Thanks

Replies are listed 'Best First'.
Re: delete a line by a string
by broquaint (Abbot) on Apr 02, 2003 at 13:58 UTC
    A simple one-liner coming up
    perl -i -ne 'print unless /\bsome string\b/' file
    See. perlrun for more info on perl's commandline arguments.
    HTH

    _________
    broquaint

      Hi. Thank for the quick response. I wanted to know if you can give me the code as a part of a script and not as a stand alone command Thanks Ahead
        Assuming your file isn't too big (i.e will comfortably fit in memory)
        open(my $fh, "<+", "your_file") or die("ack: $!"); my @data = <$fh>; seek $fh, 0, 0; my $newsize = 0; for(@data) { next unless /\bsome string\b/; print $fh; $newsize += length; } truncate $fh, $newsize; close $fh;
        Or if the file is too big to be read entirely into memory
        open(my $read, "<", "your_file") or die("ack: $!"); open(my $write, ">", "tmpfile$$") or die("ack: $!"); while(<$read>) { next unless /\b some string\b/; print $write; } close $read; close $write; rename $write, $read;
        See. perlopentut and perlfunc for more info.
        HTH

        _________
        broquaint

        while (my $line = readline STDIN) { # If the line matches the expression then skip the line. # Note the use of /x if ( $line =~ /\b word \b/x ) { # Skip to the the next iteration next; } print $line; }

        Save the code below to x.pl. Invoke with 'perl x.pl <filename>...'.

        #!/usr/bin/perl -i -n print unless /\bsome string\b/;
        90% of every Perl application is already written.
        dragonchild
Re: delete a line by a string
by diotalevi (Canon) on Apr 02, 2003 at 13:59 UTC
    perl -ibak -ne '/\bword\b/ or print' filename
Re: delete a line by a string
by Improv (Pilgrim) on Apr 02, 2003 at 15:43 UTC
    Pardon me for sacrelige, but if you're on unix, you could just do:
    cat mydata | grep -v "my strange string" > my_new_data
    (note that it's not safe to output to the same file you're inputting from)

      Yay, my first "Useless use of cat" award! This works just as well:

      grep -v "my strange string" mydata > my_new_data
      by this command I'm getting the exact opposit result from the one I wanted I'm getting a file that contains only the lines I wanted to delete. what good does that do?
Re: delete a line by a string
by vivapl (Acolyte) on Apr 02, 2003 at 20:39 UTC
    This shall work: grep -v "string to delete" file > newfile newfile should now contain all lines except the ones with the "string to delete".

Log In?
Username:
Password:

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

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

    No recent polls found