Search and Replace in multiple files

Hello,

I have hundreds of files in which I need to change email address. Here is what I am trying to do:

  1. All text files are in a directory "a"
  2. In the text file, I want to replace email address for preparer. All these lines start with {{PreparerEmail and end with }}. The email addresses on these files are different.

for e.g.

Line in file1 is
{{PreparerEmail abc@abc.com}}
Line in file2 is
{{PreparerEmail bcd@xyz.com}}
Line in file 3 is
{{PreparerEmail pqr@yyy.com}}
  1. I want to replace these emails in all the files within a directory with a common email address for e.g aaa@bbb.com
    The expected end result for the sample lines provided earlier would be
Line in file1 is
{{PreparerEmail aaa@bbb.com}}
Line in file2 is
{{PreparerEmail aaa@bbb.com}}
Line in file 3 is
{{PreparerEmail aaa@bbb.com}}

Any assistance is greatly appreciated.

Depending on your version of Unix, try this:

find . -name "file*.txt" -exec sed -i 's/^{{PreparerEmail.*}}$/{{PreparerEmail aaa@bbb.com}}/g' "{}" \;
cat file1.txt
{{PreparerEmail aaa@bbb.com}}

cat file2.txt
{{PreparerEmail aaa@bbb.com}}

cat file3.txt
{{PreparerEmail aaa@bbb.com}}

mfj.

I just tried your command, but got the following message:

sed: illegal option -- i

same message repeated - one for each file.

Also, if there were multiple occurrences of this line {{PreparerEmail
would the command replace email addresses for all occurrences?

Your system is not supporting -i option in sed .. Try with the below then

for i in *.txt ; do printf ",s/^{{PreparerEmail.*}}$/{{PreparerEmail aaa@bbb.com}}\nw\nq\n" | ed $i ; done