#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Question Read variable from data file and pass to search and replace script


    Hello,

    I'm using the below script to search and replace string in a folder. How can i modify the code to read read multiple $searchname and $replacename from a text file and replace the words?

    here are the folders: main folder: /USERS/path/to/test/

    inside test folder : data.txt original/ script.sh tmp/

    inside original folder : file1.txt file2.php ...........

    inside data.txt: a1 a2 b1 b2 c1 c2

    script for search and replace:


    Code:
     
    
     !/bin/bash
    
     FOLDER="/Users/path/to/test/original/"
     echo "******************************************"
     echo enter word to replace
     read searchterm
     echo enter word that replaces
     read replaceterm
    
    
    
       for file in $(grep -l -R $searchterm $FOLDER) 
    
    
          do
    
       sed -e "s/$searchterm/$replaceterm/g" $file > /Users/path/to/test/tmp/tempfile.tmp 
           mv /Users/path/to/test/tmp/tempfile.tmp $file
    
           echo "Modified: " $file
    
        done
    
    
    
    
       echo " *** All Done! *** "
    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    You can have, if memory serves, multiple edit commands using multiple -e parameters. You could have a first loop, going through your file that contains search/replace pairs and build up a command line string to use for sed, then just plug that in where you have your current command.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo