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

    Join Date
    Jan 2012
    Rep Power

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


    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:

     echo "******************************************"
     echo enter word to replace
     read searchterm
     echo enter word that replaces
     read replaceterm
       for file in $(grep -l -R $searchterm $FOLDER) 
       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
       echo " *** All Done! *** "
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Rep Power
    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