Monday, May 08, 2006

To SED or to PERL

I found a feature in SED where your regular expression delimeters are not required to be / they can be anything, as long as they are paired up.

Search and replace a string in a file when the new string contains forward slashes '/' as in a file path


Using SED:

sed -e 's&@ORACLE_BASE@&'$ORACLE_BASE'&g' test.txt > test2.txt

[oracle@server scripts]$ clear
[oracle@server scripts]$ cat test.txt
Peter says, "the oracle base is at: $ORACLE_BASE"
[oracle@server scripts]$ sed -e 's&@ORACLE_BASE@&'$ORACLE_BASE'&g' test.txt > test2.txt
[oracle@server scripts]$ cat test2.txt
Peter says, "the oracle base is at: /u01/app/oracle"
[oracle@server scripts]$


perl -p -i.bak -e 's{\@ORACLE_BASE\@}{$ENV{ORACLE_BASE}}' test.txt