Search and Replace with Confirmation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Replace with Confirmation
# 8  
Old 02-02-2010
This is the script for searching and replacing string in dir:

Code:
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 Smilie

Last edited by Franklin52; 02-02-2010 at 06:40 AM.. Reason: code tags, please...
# 9  
Old 02-02-2010
This is not something to do in a jiffy without scripting experience,

Try this approach:

Code:
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"
# 10  
Old 02-03-2010
Thanks Franklin,

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

Thanks for the suggestion.
Abhinav :-)

---------- 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

Code:
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


Last edited by Abhinav Tyagi; 02-02-2010 at 11:57 AM..
# 11  
Old 02-04-2010
Hi,

Thanks to Franklin, my script started working !!! Smilie

Only one issue left Smilie

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???



Code:
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
# 12  
Old 02-04-2010
Quote:
Originally Posted by Abhinav Tyagi
Hi,

Thanks to Franklin, my script started working !!! Smilie

Only one issue left Smilie

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???

Thanks
Abhinav
Quote the variables in this line:
Code:
nawk -v find="$f" -v replace="$r"

# 13  
Old 02-04-2010
It's Working Now !!!SmilieSmilie

Thanks SOOOOOOO MUCH
Abhinav

Last edited by Abhinav Tyagi; 02-04-2010 at 07:08 AM..
# 14  
Old 02-04-2010
Quote:
Originally Posted by Abhinav Tyagi
It's Working Now !!!SmilieSmilie

Thanks SOOOOOOO MUCH
Abhinav
Glad to know it's working now.Smilie

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

2. UNIX for Dummies Questions & Answers

Help with search and replace or search only of / in vi

Hi all, I am editing a config file in vi that has a / on it. At the moment, search and replace looks alright as am able to use a # as a temporary separator, i.e. :,$s#/u01/app#/u02/app#g For doing a search, I have to escape the / do. So if I want to search for /u01/app, I am having to do... (2 Replies)
Discussion started by: newbie_01
2 Replies

3. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

4. Shell Programming and Scripting

Confirmation while deleting a file

Hi All, While am deleting a file in unix by using the command rm it's deleting the file directly IT's have to ask the user while deleting like do you want to delete filename y or N ? Kindly help on this. Thanks Thelak (2 Replies)
Discussion started by: thelakbe
2 Replies

5. Shell Programming and Scripting

twice confirmation in the script

from one script invoke another script will need to do double confirmation like: 'y' and 'y' exp: echo 'y' | /a/b/c.sh only input 1 'y' in the c.sh, but the c.sh required 2 'y' (twice confirmation) is there anyway get resolve this issue? please help (2 Replies)
Discussion started by: kinx
2 Replies

6. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

7. Web Development

Pop up Confirmation Box

Hi, I was writing a simple web application using Perl -CGI. When users try to do some operations, I wanted like a pop-up confirmation box. Is this possible with Perl-CGI? Thanks in advance. Regards, garric (6 Replies)
Discussion started by: garric
6 Replies

8. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

9. Forum Support Area for Unregistered Users & Account Problems

need email confirmation resent

we had a power outage shortly after i registered... so my mail server was down for a while... could i get the email confirmation for hackware --> resent please...? thanx... ...william.o.yates...email removed (3 Replies)
Discussion started by: hackware
3 Replies

10. UNIX for Dummies Questions & Answers

Mail delivery confirmation

If I am sending mail with this command: mail .......@whatever.com < filename, is it possible to get delivery confirmation? Thanks (3 Replies)
Discussion started by: CSGUY
3 Replies
Login or Register to Ask a Question