Replace text

Hi,
I have 50 shells in a particular directory. In each shell, we have a variable
MAIL_SUCCESS=abc@xyz.com,def@xyz.com,emi@xyz.com

The variable could be different. Now my requirement is I've to replace abc with def in all the shells present. I have been able to get the command for that:

find . -name "*.sh"|xargs perl -pi -e 's/abc\@xyz.com/def@xyz.com/g'

But, the problem is, if the variable already contains def@xyz.com, then i just need to remove abc@xyz.com. In such case, I've to give the command:

find . -name "*.sh"|xargs perl -pi -e 's/abc\@xyz.com,?//g'

How do I join both commands such that, if abc@xyz.com and def@xyz.com appear in the same line, then remove abc@xyz.com , else replace abc@xyz.com with def@xyz.com

Try:

find . -name "*.sh"|xargs perl -i -pe '/def\@xyz.com/&&s/abc\@xyz.com,?//g;s/abc\@xyz.com/def\@xyz.com/g'

Thanks a lot.

I have just made a minor change:

find . -name "*.sh"|xargs perl -i -pe '/def\@xyz.com/&&s/,abc\@xyz.com,?//g;s/abc\@xyz.com/def\@xyz.com/g'

If abc@xyz.com is in the end, we had a trailing comma. Now, even that has been handled.

Could you please explain what you did?

/def\@xyz.com/&&s/,abc\@xyz.com,?//g;

Red checks if def@xyz.com is present in the line, and if it is, then s// is executed (removing abc@xyz.com).

s/abc\@xyz.com/def\@xyz.com/g

This is quite obvious. It will replace any one abc@xyz.com in the line, that wasn't removed by previous command.