Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

need help in editing multiple word in a file

by vkp (Novice)
on Nov 18, 2012 at 18:23 UTC ( [id://1004447]=perlquestion: print w/replies, xml ) Need Help??

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

i want to replace some words in a file. In words are present in whole file. But starting of each line in file is by different word. so i am using a file in which first character of line at which i wanna do replacement is written and along with it replaced word is given ( i name it match file) . i m using following logic. but here is problem with it that i don't know in which line the first match-replace combination in match file is present in input file. so i need to reopen this file. In this way code will copy all the lines in new file that i m forming with this.

#!/usr/bin/perl use warnings; my $source = shift @ARGV; my $destination = shift @ARGV; my $matchfile=shift @ARGV; open(OUT, '>', $destination) or die $!; open(MF, '<', $matchfile) or die $; while(<MF>) { @DATA=split(/ /,$_); $matcher=$DATA[0]; $replacer=$DATA[1]; open(IN, '<', $source) or die $!; while(<IN>) { if($_=~/^$matcher/) { if(s /legend/$replacer/) { print OUT $_; } } else { print OUT $_; } } } close IN; close OUT; close MF;

Replies are listed 'Best First'.
Re: need help in editing multiple word in a file
by lancer (Scribe) on Nov 18, 2012 at 21:06 UTC

    I rewrote the code to make it work as described in your post.


    replace_with_matches.pl

    #!/usr/bin/perl use warnings; use strict; my $source = shift @ARGV; my $destination = shift @ARGV; my $matchfile = shift @ARGV; my @matches = (); open (MF, '<', $matchfile) or die $!; while (<MF>) { push @matches, $_; } close MF; # have a list of all matches my @input = (); open (IN, '<', $source) or die $!; while (<IN>) { push @input, $_; } close (IN); # have all input lines in an array my @output = (); NEXT_INPUT_LINE: for my $input_line (@input) { # check every input line 1 time NEXT_MATCH_PAIR: for my $match_pair (@matches) { my @DATA = split (/\s/, $match_pair); my $matcher = $DATA[0]; my $replacer = $DATA[1]; # check all match pairs against 1 input line if ($input_line =~ /^$matcher/) { # make replacement $input_line =~ s/legend/$replacer/; push @output, $input_line; # jump to next input line next NEXT_INPUT_LINE; } } # if it gets here, there was no replacement # copy input line to output push @output, $input_line; } # have all replaced output in @output # write @output to $destination open (OUT, '>', $destination) or die $!; for my $output_line (@output) { print OUT $output_line; } close OUT;



    Sample input files:


    source.txt

    lancer is a legend asdf asdfwerg asdf wergdfv legend pieces of trash line 4 is cool and makes no sense time for legends time for christmas jesus is a legend


    matches.txt

    l jerk j myth



    Sample output:


    result.txt

    lancer is a jerk asdf asdfwerg asdf wergdfv legend pieces of trash line 4 is cool and makes no sense time for legends time for christmas jesus is a myth

      thanks for ur kind help. I m new in coding, it is really appreciable to explain each step for beginner

Re: need help in editing multiple word in a file
by pvaldes (Chaplain) on Nov 18, 2012 at 21:12 UTC

    >I want to replace some words in a file

    ok

    >In words are present in whole file. But starting of each line in file is by different word so i am using a file in which first character of line at which i wanna do replacement is written

    mmmh? I wonder how you could have a file with unwritten characters. Do you want to match only the first character?

    >and along with it replaced word is given ( i name it match file)

    uh?

    sorry, I get lost, can't understand what you want to do exactly

    Could we have a small example with some data

      consider this example: lion is brave animal. cat is brave animal. dog is brave animal. in this i want to change brave by frightened only for cat. so i need to match cat and replace brave with frightened. So match file is like: cat frightened . Thanks.

        Ok, so how do you propose deciding which word in the string:

        cat is brave animal

        should be replaced by 'frightened'?

        In other words, is this also acceptable:

        cat frightened brave animal

        ?

        Update: de-gobbledegooked, I hope...

Re: need help in editing multiple word in a file
by aitap (Curate) on Nov 18, 2012 at 18:52 UTC
    Do you mean seek(IN,0,0);? This will place you at the start of the IN file, allowing you to read it again. See seek for more info.
    Sorry if my advice was wrong.
Re: need help in editing multiple word in a file
by pvaldes (Chaplain) on Nov 20, 2012 at 12:43 UTC

    I need to match cat and replace brave with frightened

    while (<DATA>){ next unless /\bcat\b/i; s/brave/frigthened/; print $_; } __DATA__ My Cat is brave but ocicat is also brave and dog is brave catwoman wonderbrave

    You can achieve similar results using grep

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-03-19 03:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found