P
pavan734
Hello,
I have a perl script running on a file1 that searches for
pattern1 and replaces with pattern2 and writes to file2. But this will
do all the substitutions. Here is a code that searches for hi in
source.txt and replaces with bye in target.txt.
#!/cadappl/bin/perl
open(INFILE, "source.txt") or die "Can't open source.txt: $!";
open(OUTFILE, ">target.txt") or die "Can't open target.txt: $!";
while ( <INFILE> ) {
if($_ ~=/\bhi\b/)
{
print $_ ; //prints the line containing hi to screen
s/\bhi\b/\bbye\b/g ;
}
print OUTFILE; //writes to target.txt
}
My requirement is before doing replace the script must ask the user
whether to do the substitution or not. That is it must ask the user
"press y if u want to substitute else press any character."
I have a perl script running on a file1 that searches for
pattern1 and replaces with pattern2 and writes to file2. But this will
do all the substitutions. Here is a code that searches for hi in
source.txt and replaces with bye in target.txt.
#!/cadappl/bin/perl
open(INFILE, "source.txt") or die "Can't open source.txt: $!";
open(OUTFILE, ">target.txt") or die "Can't open target.txt: $!";
while ( <INFILE> ) {
if($_ ~=/\bhi\b/)
{
print $_ ; //prints the line containing hi to screen
s/\bhi\b/\bbye\b/g ;
}
print OUTFILE; //writes to target.txt
}
My requirement is before doing replace the script must ask the user
whether to do the substitution or not. That is it must ask the user
"press y if u want to substitute else press any character."