using variable in search and replace



Hi all,
I am trying to search for a string in FILE1 and then split it and then
use the result to search FILE2

#!/usr/bin/perl
print "This is test perl program\n";
open ( FILE1,"</test/file1.pl") or die "cannot open $?:$!";
while (<FILE1>){
$i= s/apple/APPLE/;
#just replacing apple with APPLE
#this works fine
if($i){
$test1=$_;
}
}
close FILE1;
print "test1 =$test1\n";
#this has the the whole line which is of #the form a b

($a,$b)=split(/\s/,$test1,2);
print "b=$b\n";

open (FILE2,"</temp/file2.pl") or die "cannot open $?:$!";
while (<FILE2>){
$i= s/$b/MANGO/e;
# This part doesn't work

print $_;
if($i){
$test2=$_;

}
}
close FILE2;
#print "$test2";
($c,$d)=split(/\s/,$test2,2);
print "d= $d\n";

the contents of file1he
ap878ple mango
1apple me45ango
a4pple mat5ngo
apple mango
a2pple ma43ngo
a4pple mat5ngo

the contents of file2
mandfgo andwefrwehra
maafweewngo andhvwera
mango pineapple
masvffngo anewfwdhra

Main purpose of the code is
given apple I want to search file1 and file2 and find pineapple.
the variable $b doesnt seem to work well in the search.

Thanks in advance.
Ravi

.



Relevant Pages

  • Re: simple ln question
    ... that makes it so the contents of file1 are the same as of file2, and if you change the contents of 1, the other ... prints the inode number for the file foobar. ...
    (Fedora)
  • RE: data conundrum
    ... in your spens you'll want to keep field names consistent. ... the specs will be the most tedious, but once theyr're done once they're done. ... in other words, you have file1, file2, file3..... ...
    (microsoft.public.access.externaldata)
  • RE: data conundrum
    ... let's say file1 looks like this: ... and let's say file2 looks like this: ... then you will probably have to create import specs for all 100 ... create from the .csv files) as opposed to using a form, ...
    (microsoft.public.access.externaldata)
  • Re: Need Help
    ... eg: file1, file2, file3 etc ... into, max 5-6, chunk files and then loading these chunk files into ...
    (comp.unix.shell)
  • Re: egrep question
    ... I personally would not use grep but awk instead, especially if file1 and file2 ... If you use egrep, ... Tom Sawyer in file2. ...
    (AIX-L)