Using Sed For Search and Replace

I recently had a requirement to change the email for all shell scripts from personal (me@gmail.com) to a DBA list (dba@gmail.com).
Typically, directories for shell scripts are scheduled using crontab. If shell scripts aren’t scheduled with crontab, then we’d need to find all the locations for shell scripts – this is beyond the scope of this post.
First, I checked crontab to find the shell scripts located in the directory /home/vagrant/scripts.
[vagrant@oracle-12201-vagrant ~]$ crontab -l 5 4 * * * /home/vagrant/scripts/test.sh something > /tmp/test.out 2>&1 [vagrant@oracle-12201-vagrant ~]$ [vagrant@oracle-12201-vagrant ~]$ crontab -l|grep -v '#'|grep sh|awk '{print $6}'|sort -u /home/vagrant/scripts/test.sh [vagrant@oracle-12201-vagrant ~]$
Next, I reviewed the shell scripts from the directory.
### Note: dt.sh is not schedule from crontab and resides in the same directory. [vagrant@oracle-12201-vagrant scripts]$ ls -l total 12 -rwxrwxr-x. 1 vagrant vagrant 25 Feb 4 21:15 dt.sh -rwxrwxr-x. 1 vagrant vagrant 20 Feb 4 21:14 test.sh [vagrant@oracle-12201-vagrant scripts]$
I created edit_email.sh to search and replace email from all shell scripts (.sh) using sed (stream editor).
Because the AIX version I tested doesn’t support in-place search and replace using sed, I had to create a temporary file.
The implementation below will work for most operating systems:
[vagrant@oracle-12201-vagrant scripts]$ cat ./edit_email.sh for infile in $(grep 'me@gmail.com' *.sh|grep sh|awk -F':' '{print $1}'|sort -u|grep -v `basename $0`) do echo $infile sed 's/\bme@gmail.com\b/dba@gmail.com/g' $infile > tmp.$$ mv tmp.$$ $infile chmod 755 $infile grep 'gmail.com' $infile done [vagrant@oracle-12201-vagrant scripts]$
Finally, I ran edit_email.sh.
[vagrant@oracle-12201-vagrant scripts]$ cat dt.sh date echo me@gmail.com echo tome@gmail.com [vagrant@oracle-12201-vagrant scripts]$ ### Note: tome@gmail.com and ame@gmail.com did not get updated. [vagrant@oracle-12201-vagrant scripts]$ ./edit_email.sh dt.sh echo dba@gmail.com echo tome@gmail.com test.sh export PAGER_EMAIL="ame@gmail.com" [vagrant@oracle-12201-vagrant scripts]$
I hope you can see that using sed to search and replace is more more efficient and less prone to errors than editing each script manually.
If you have any thoughts or questions, please leave them in the comments.