Avatar of AXISHK
 asked on

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 :

/eng               /chi
engwrd         chiwrd
/oci/pass      /bci/pass

Is it possible to archieve this through a bash script ?

LinuxLinux Distributions

Avatar of undefined
Last Comment

8/22/2022 - Mon
Steven Vona

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.

Do you mind to put comment in each line so that I can follow your logical ?

Thank again.
Steven Vona

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

Your help has saved me hundreds of hours of internet surfing.