Search and Replace with Confirmation

Hi,

I have created a script which searches and replace the searched text from any file in directory.
I want to add a functionality where before replacing the text, script shows the line with the searched text and ask for user confirmation before replacing the text.
Can this be done using SED or any other command.

I have used SED for searching and replacing the text in my script.

Thanks
Abhinav

You can do a GREP of the string to be changed and then ask.
After you can sed if confirm = Yes or something like.

You can also use SED without modifying the file before.

You could post your script for suggestions.

Thanks for your reply,
but by using grep how can I replace a single line or text within that line using SED command?
what would be the syntax?

He is saying to first search by grep and ask for confirmation and if yes then run sed.something like this.

if [ grep /abc/ filename];then
   if [ "$ANSWER" == "Y" ];then
     #Run the Sed Command
   else
      #Exit the loop
   fi
fi

I understand your point
but if I use my sed command in

if [ grep /abc/ filename];then
   if [ "$ANSWER" == "Y" ];then
     #Run the Sed Command
   else
      #Exit the loop
   fi
fi

then this would be asking confirmation for replacing text at some position or whole in a file...

I want to know how can I use SED command to replace each occurence only with confirmation from the user.

eg.

let this be some text in file

apple boy cat dog
boy dog apple apple cat
...
...

script should display

do you want to replace "apple" with "APPLE()" in line
apple boy cat dog (y/n)

say n

again
do you want to replace "apple" with "APPLE()" in line (for first occurrence)
boy dog apple apple cat (y/n)

say y

again
do you want to replace "apple" with "APPLE()" in line (for second occurrence)
boy dog APPLE() apple cat (y/n)

say n

and so on

Can this be achieved using SED or any other command ???

Need more info.
Can some one tell me how I can replace text as mentioned above?

Thanks
Abhinav

So far what have you tried or done? More, it's very home/classwork looking dont you think?

why dont you show to us then...

So why ask more info?

This is the script for searching and replacing string in dir:

for file in $(grep -l "$find" $path)   #finds files with searched value 
      do
           echo " "
           echo "Modifying : " $file
           sed  's/'"$find"'/'"$replace"'/g' $file > temp.txt
           mv temp.txt $file
           echo " "
           echo "Modified : " $file
      done

Now I need to ask for user confirmation before replacing by showing each line (with $find) from each file in dir and replace only those LINES where user enters 'y'.

Please refer my previous post for example.

Any suggestion is appreciated.

Thanks
Abhinav :slight_smile:

This is not something to do in a jiffy without scripting experience,

Try this approach:

awk '
BEGIN {
  printf "Word to replace? "
  getline s < "-"
  printf "Replace with? "
  getline r < "-"
}
{ while(index($0,s)) {
    print "Replace the first " s " with "r"?"; print
    getline yn < "-"
    if(toupper(yn)=="Y"){
      sub(s,r)
    }
    else {
      sub(s,"_")
    }
  }
}
{
  gsub("_",s)
  print > "newfile"
}' filename

Play around and adjust it according to your preference.

The input file is "filename" and the output goes to "new_file"

Thanks Franklin,

I will try this. But I don't know about awk command. need to study it first.

Thanks for the suggestion.
Abhinav :slight_smile:

---------- Post updated 2010-02-03 at 12:34 PM ---------- Previous update was 2010-02-02 at 04:28 PM ----------

Hi Franklin

I want to transfer variable from shell to nawk.

The following code not transferring the variable.

Also when your version of code runs, then

APPLE() is replaced apple in line as apple(), not replacing () .
Any suggestions?

Thanks
Abhinav

for file in $(grep -h -l "$find" $path)
do
nawk -v find=$f '  #not working
BEGIN {
  #printf "Word to replace? "
  #getline find < "-"
  #find=$f
  printf "Replace with? "
  getline replace < "-"
  print find
  print replace
}
{ 
   while(index($0,find)) {
    print "Replace "find" with "replace"?"; print
    getline yn < "-"
    if(toupper(yn)=="Y"){
      sub(find,replace)
    }
    else {
      sub(find,"_")
    }
  }
  
}
{
  gsub("_",find)
  print > "temp.ftxt"
}' $file

mv temp.ftxt $file

Hi,

Thanks to Franklin, my script started working !!! :slight_smile:

Only one issue left :confused:

sub(s,r) replaces only one word, not even words separated by space.
I want to replace say s="apple boy cat" with r="APPLE BOY CAT DOG"

any thing I am missing???

nawk -v find=$f -v replace=$r '
BEGIN 
{
  ...
  ... 
}
{ 
 while(index($0,find)) 
      {
          print "Replace "find" with "replace" (y/n) ?"; print
          getline yn < "-"
          if(toupper(yn)=="Y")
             {
              sub(find,replace)###### ONLY REPLACING ONE WORD
                                           ###### NOT EVEN WORDS SEPARATED BY SPACE
             }
          else 
             {
                sub(find,"_")
             }
       }
  }
  {
  gsub("_",find)
  print > "temp.ftxt"
  }' $file
  mv temp.ftxt $file

Thanks
Abhinav

Quote the variables in this line:

nawk -v find="$f" -v replace="$r"

It's Working Now !!!:cool::b:

Thanks SOOOOOOO MUCH
Abhinav

Glad to know it's working now.:slight_smile:

Regards

sub(s,r)

not replacing special characters like ()
script runs infinitly.

Thanks
Abhinav

you can give vim a try ... use the substitute facility in that.

:%s/search/replace/gc

not using vi or vim

If the variable s contains parentheses "()" you must escape them before the substitution:

sub("\(\)","\\(\\)",s); sub(s,r)

not working :frowning:

text may contain () or (abcd).. or any other special character, depends totally on user.
as

sed s'/$s/$r/'

replaces any character with any.

Thanks
Abhinav