 |
|
Linux command to change all values within files in an Oracle directory?
Oracle Database Tips by Donald Burleson |
Question: What Linux or UNIX command can I use to change
all occurrences of a string within a directory? Do I use sed, awk, or
what?
Answer:
The sed command is used to make global changes to strings in UNIX files.
For example, below we have a utility that will change all strings in a
directory from one string to another. This is sort of a ?change all?
utility within UNIX. The format of the sed command is:
sed/oldstring/newstring > new file location
The sed utility always makes a new copy of every file it changes, so special
care is required to make an in-place change. Note the sed line in this
script where sed changes the old string to the new string in all files in
the directory. Note that this script makes a backup of the files in a tmp
sub-directory before issuing the change.
WARNING: This is a VERY POWERFUL script,
not to be used by beginners. Use this script at your own risk:
1 - copy-paste this script as chg_all.sh on your
server in the home directory where you wish to make cascading changes.
2 - Issue a chmod +x chg_all.sh to make it
executable.
3 - vi the file and change the oldstring and newstring
values. NOT you can put spaces in the command like so:
sed -e 's/ 2004 / 2015 /g' < $f > $tmpdir.new/$f
4 - Save the file from vi "<esc> : x" and execute the
command:
./chg_all.sh
chg_all.sh is below:
#!/bin/ksh
tmpdir=tmp.$$
mkdir $tmpdir.new
for f in $*
do
sed -e 's/oldstring/newstring/g' < $f > $tmpdir.new/$f
done
# Make a backup first!
mkdir $tmpdir.old
mv $* $tmpdir.old/
cd $tmpdir.new
mv $* ../
cd ..
rmdir $tmpdir.new
See my notes on Oracle directories here.
 |
If you like Oracle tuning, see the book "Oracle
Tuning: The Definitive Reference", with 950 pages of tuning tips and
scripts.
You can buy it direct from the publisher for 30%-off and get
instant access to the code depot of Oracle tuning scripts. |