Search and replace with confirmation

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."
 
B

Bart Van der Donck

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.
[...]
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."

This demonstrates the principle:

#!perl
use strict;
use warnings;
print "\nType 'y' to proceed or any other character to cancel: ";
my $character = <>;
chop $character;
if (lc $character eq 'y') {
print "\nOK to do substitution\n";
}
else {
print "\nNot OK to do substitution\n";
}
 
P

pavan734

Thank you Bart Van der Donck.
Bart said:
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.
[...]
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."

This demonstrates the principle:

#!perl
use strict;
use warnings;
print "\nType 'y' to proceed or any other character to cancel: ";
my $character = <>;
chop $character;
if (lc $character eq 'y') {
print "\nOK to do substitution\n";
}
else {
print "\nNot OK to do substitution\n";
}

--
Bart

" I don't like computer code:
it always does what I say, not what I want ! "
 
A

A. Sinan Unur

(e-mail address removed) wrote in @j73g2000cwa.googlegroups.com:
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."

See

http://www.unur.com/comp/ppp/delallspam.html

for an example.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
T

Tad McClellan

I have a perl script running on a file1 that searches for
pattern1 and replaces with pattern2


There *is no* second pattern.

s/// has ONE pattern, and one replacement *string*.

s/\bhi\b/\bbye\b/g ;
^^ ^^
^^ ^^ these are backspaces, not word boundaries,
because they are in a string, not in a regex

Have you even tried this code?

My requirement is before doing replace the script must ask the user
whether to do the substitution or not.


-------------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = "hi there, the cost is too hi";
my $find = 'hi';
my $replace = 'bye';

s/\b($find)\b/ confirm($_, $find, $replace, pos) /ge;

print "$_\n";


sub confirm {
my($string, $pattern, $replacement, $position) = @_;

print "$string\n";
print ' ' x $position, '^' x length($pattern), "\n";

print "replace? (y/n) ";
chomp(my $ans = <STDIN>);

return $ans =~ /^(yes\b|y$)/ ? $replacement : $pattern;
}
-------------------------------


Or, if you don't mind your program being slower to execute:

-------------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = "hi there, the cost is too hi";
my $find = 'hi';
my $replace = 'bye';

s/\b($find)\b/ confirm($find, $replace) /ge;

print "$_\n";


sub confirm {
my($pattern, $replacement) = @_;

print "$`<<$&>>$'\n";

print "replace? (y/n) ";
chomp(my $ans = <STDIN>);

return $ans =~ /^(yes\b|y$)/ ? $replacement : $pattern;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top