sophix has asked for the wisdom of the Perl Monks concerning the following question:
Hi all,
I am stuck with this search&replace problem and would appreciate your help.
What I would like to do is to find the ids in a file and replace them with their descriptions provided in another text file.
Here is the reference file - tab-delimited
301609239 kitchen 114672573 study room 20531742 living room 89242146 terrace 301781720 balcony
And here is the file to be read and in which the replacements to be included
ABCDE + + +P237S S{301609239} + + + +R372W W{114672573} + + + +K576R R{20531742} R{89242146} + + + +R1059Q Q{126723431} Q{6934272} STRKE +T216I I{301781720} I{148237434} + + + +V275I I{149632297} I{47224534} + + + +R13H H{126333615} + + + +F113L L{301781720} L{148237434} + + + +G135S S{147902132} S{47224534} S{125864042} S{107921834} + + + +T307A A{224050516} A{126333615} + + + +L217F F{149632297} F{147902132}
The desired output would look like this:
ABCDE + + +P237S S{kitchen} + + + +R372W W{study room} + + + +K576R R{living room} R{terrace} + + + +R1059Q Q{126723431} Q{6934272} STRKE +T216I I{balcony} I{148237434} + + + +V275I I{149632297} I{47224534} + + + +R13H H{126333615} + + + +F113L L{301781720} L{148237434} + + + +G135S S{147902132} S{47224534} S{125864042} S{107921834} + + + +T307A A{224050516} A{126333615} + + + +L217F F{149632297} F{147902132}
And here is the script that I wrote but it does not work - not even generating error
#!/usr/bin/perl -w my $data = 'reference.txt'; open INFILE, '<', $data; while(<INFILE>) { my $line = $_; chomp($line); my ($id, $description) = split /\t/, $line, 2; my $data2 = "readthisfile.txt"; open INFILE2, "<", $data2; while(<INFILE2>) { my $line2 = $_; chomp($line2); $line2 =~ s/$id/$description/g; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Search & Replace Across Two Files
by NetWallah (Canon) on Nov 21, 2010 at 15:06 UTC | |
|
Re: Search & Replace Across Two Files
by JavaFan (Canon) on Nov 21, 2010 at 11:45 UTC | |
|
Re: Search & Replace Across Two Files
by generator (Pilgrim) on Nov 21, 2010 at 12:01 UTC | |
by tod222 (Pilgrim) on Nov 21, 2010 at 23:28 UTC | |
|
Re: Search & Replace Across Two Files
by Generoso (Prior) on Nov 22, 2010 at 04:01 UTC |