Skip to content

Insight and analysis of technology and business strategy

Using Sed For Search and Replace

I recently had a requirement to change the email for all shell scripts from personal ( to a DBA list (

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/ 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
[vagrant@oracle-12201-vagrant ~]$

Next, I reviewed the shell scripts from the directory.

### Note: 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
-rwxrwxr-x. 1 vagrant vagrant  20 Feb  4 21:14
[vagrant@oracle-12201-vagrant scripts]$

I created 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 ./
for infile in $(grep '' *.sh|grep sh|awk -F':' '{print $1}'|sort -u|grep -v `basename $0`)
echo $infile
sed 's/\\b/' $infile > tmp.$$
mv tmp.$$ $infile
chmod 755 $infile
grep '' $infile
[vagrant@oracle-12201-vagrant scripts]$

Finally, I ran

[vagrant@oracle-12201-vagrant scripts]$ cat
[vagrant@oracle-12201-vagrant scripts]$

### Note: and did not get updated.

[vagrant@oracle-12201-vagrant scripts]$ ./
export PAGER_EMAIL=""
[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.

Top Categories

  • There are no suggestions because the search field is empty.

Tell us how we can help!