Tag Archives: string replacement

String replacement at UNIX or LINUX command prompt

As an Oracle DBA you will come across many situations where you need to replace string text within OS layer files. This procedure covers the use of Perl command to perform such a task. This procedure will work in both UNIX and LINUX.

1. Logon to you Oracle server as the owner of the files you wish to perform a string replacement on.

2. We will use the command (“perl -pi -e ‘s/{old_string}/{new_string}/g’ {file_name}”) to perform our string replacement.

3. In this example we will replace all of the references to the string “MARS” with the string “EARTH” in any file that ends in test*sh, the command will appear as:

perl -pi -e ‘s/MARS/EARTH/g’ test*sh

4. List all of the contents of files of the format test*sh with the cat command.

mylinux:>cat test*sh
MARS
MARS
MARS
MARS
mylinux:>

5. Execute your perl command in the same directory where your the files are located.

mylinux:>perl -pi -e ‘s/MARS/EARTH/g’ test*sh mylinux:>

6. List all of the contents of the files with format of test*sh with the cat command.

mylinux:>cat test*sh
EARTH
EARTH
EARTH
EARTH
mylinux:>

As you can see above the test string “MARS” has been replaced with “EARTH”. This completes replacement of text string with Perl.

Larry Catt, OCP 9i, 10g
oracle@allcompute.com
www.allcompute.com