0

i have more than 1000 files and want to replace a special text in all of them with another phrase.

how can i do it by shell script in linux?

5 Answers 5

5
sed -i 's/old-word/new-word/g' *.txt

http://www.cyberciti.biz/faq/unix-linux-replace-string-words-in-many-files/

Sign up to request clarification or add additional context in comments.

Comments

1

Something like this:

for file in *.txt
do
    cp $file $file.tmp
    cat $file.tmp | sed 's/foo/bar/g' > $file
done

1 Comment

You can use -i to edit the files in place, so there is neither a need for a temporary file nor for a loop.
1

You could also use perl:

perl -pi -e 's/find/replace/g' *.txt

Comments

1

Just bash

for file in *.txt
do
   while read -r line
   do
     case "$line" in
       "*pattern*") line="${line//pattern/new}";;
     esac
     echo "$line"
   done <"$file" > t
   mv t "$file"
done

Comments

0

Use sed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.