Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Remove string from file

by bran4ever (Novice)
on May 21, 2006 at 13:17 UTC ( [id://550784]=perlquestion: print w/replies, xml ) Need Help??

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

how do i remove a string from a file? for example, there is a text file "userlist.txt" inside that file...heres a list...for example Tom Allen etc i am trying to search a function that can delete just one line like i want to delete user "TOM"... many thanks Brandon

2006-05-21 Retitled by g0n, as per Monastery guidelines
Original title: 'regarding for remove'

Replies are listed 'Best First'.
Re: Remove string from file
by Zaxo (Archbishop) on May 21, 2006 at 13:30 UTC

    I'll suppose the key is the first word on the line,

    $ perl -pi -e's/^TOM\b.*\n//' userlist.txt $
    The regex can be adjusted for other circumstances.

    After Compline,
    Zaxo

Re: Remove string from file
by liverpole (Monsignor) on May 21, 2006 at 13:45 UTC
    perl -pi -e's/^TOM\b.*\n//' userlist.txt

    Isn't this deleting the pattern from the line, rather than deleting the line itself?

    If it's Unix/Linux, you might consider using grep or fgrep with the "-v" switch to skip lines containing a certain pattern.

    For example, to remove lines containing "TOM" anywhere in the line, use fgrep ("fixed grep") like this:

    % fgrep -v TOM userlist.txt > edited.txt
    Or to remove lines containing the word "TOM":
    % grep -v "\<TOM\>" userlist.txt > edited.txt

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Sorry guys, i meant in the script, not from command i am running a script that can add users and remove users. add users is done, but the remove users, i'm having trouble figuring out..
        What do you have so far?  Can you show some code, even it isn't quite working, so we can see which part needs to be tweaked?

        And what are the specific requirements?  Removing a line (or lines?) that exactly matches/match a given name?  Does it have to work for multiple names, or just one name?  Can the name be anywhere in the line(s)?  Can there be other names in the line, or not?  Does whitespace matter?

        If you can show us the code that you have so far, and pinpoint the exact requirements, I'm sure we can get you farther towards your goal.


        s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Remove string from file
by g0n (Priest) on May 21, 2006 at 14:10 UTC
    Here's an improved version of the code I posted in the CB in answer to your question.

    my $replacestring = "TOM"; open(my $infile,"<","inputfile.txt") or die $!; open (my $outfile,">","outputfile.txt") or die $!; while (<$infile>) { $_=~s/$replacestring//g; print $outfile $_; } close $infile; close $outfile;

    That will read the content of 'inputfile.txt', and write out the content to 'outputfile.txt', removing $replacestring along the way. The key bit is $_=~s/$replacestring//g - that replaces any instance of $replacestring in the $_ variable with blank.

    The remaining step is to rename 'inputfile.txt' as something else, and rename 'outputfile.txt' as 'inputfile.txt'.

    Update: Something else you might want to look at: your post says that you want to remove a string, but later you say a line. This code removes the entire line - I'll let you work out why ;-)

    my $string = "TOM"; open(my $infile,"<","inputfile.txt") or die $!; open (my $outfile,">","outputfile.txt") or die $!; while (<$infile>) { if ($_ !~/$string/) { print $outfile $_; } } close $infile; close $outfile;

    --------------------------------------------------------------

    "If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
    John Brunner, "The Shockwave Rider".

    Can you spare 2 minutes to help with my research? If so, please click here

      Hi Your solution seems to be great. But i have a different problem. I have a file 'A' which contains say n lines each having unique name. Now in another file 'B' I need to search for these n strings from 'A' and delete those lines in this 'B' and create a new file 'C'. Can any one help me on this
        Your post isn't going to attract much attention here at the bottom of an old thread. Could I suggest that you repost here with a more detailed version of your question (and what code you've got so far).

        --------------------------------------------------------------

        "If there is such a phenomenon as absolute evil, it consists in treating another human being as a thing."
        John Brunner, "The Shockwave Rider".

Re: Remove string from file
by ides (Deacon) on May 21, 2006 at 13:34 UTC

    What you want to do is open the original file and open a new output file say "userlist-output.txt" for writing. Then you just loop over the original file's contents and when you find the line you want to omit, simply don't output it to the new output file.

    Frank Wiles <frank@revsys.com>
    www.revsys.com

Re: Remove string from file
by ashes a (Initiate) on Dec 31, 2014 at 22:13 UTC

    Thanks for finding a solution guys and gals. Just saved me the headache of having to remove the word "remove" appended on the end of each line in a document with 300 lines.

    --
    I changed it a little bit, posted below:
    --
    #!perl print "What is the name of the file you want to remove a word ar phras +e from? \nPlease include the extension. \n"; chomp(my $rem_name = <STDIN>); print "Please enter a word ar phrase you want to remove: \n"; chomp(my $replacestring = <STDIN>); open(my $infile,"<","$rem_name") or die $!; open (my $outfile,">","outputfile.txt") or die $!; while (<$infile>) { $_=~s/$replacestring//g; print $outfile $_; } close $infile; close $outfile;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-05-29 03:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found