bash script to change texts in a list of files

I need to write a scrpt to change some text for all files stored under a particular directory and sub-directories. The script will read a file (replace.txt) and change the context of each file based on it. The file will have the following format :

replace.txt
/eng               /chi
engwrd         chiwrd
/oci/pass      /bci/pass

Is it possible to archieve this through a bash script ?

Tks
AXISHKAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

savoneCommented:
#!/bin/bash
while read line; do
        word=`echo $line | awk '{print $1}'`
        newword=`echo $line | awk '{print $2}'`

echo "WORD is $word"
echo "Replace WORD with $newword"

find /var/tmp/test -type f | xargs sed -i "s#$word#$newword#g"
done < replace.txt

Open in new window


For this to work, replace.txt has to be in the same directory as the script.  You should also replace /var/tmp/test with the directory which holds the files you want to change.

NOTE: You can remove the "echo" lines if you wish, I did that just for testing.
0
AXISHKAuthor Commented:
Do you mind to put comment in each line so that I can follow your logical ?

Thank again.
0
savoneCommented:
#!/bin/bash
# Read replace.txt line by line
while read line; do
        # Take first word of line and make it a variable - word to find
        word=`echo $line | awk '{print $1}'`
         # Take second work of line and make it a variable - replace first word with this word
        newword=`echo $line | awk '{print $2}'`

echo "WORD is $word"
echo "Replace WORD with $newword"

# Find every file in /var/tmp/test then swap first word for second word
find /var/tmp/test -type f | xargs sed -i "s#$word#$newword#g"
#done
done < replace.txt
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AXISHKAuthor Commented:
Tks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.