search and replace in many files

I have a directory full of files. Most of the code in the files is the same accept for a few parameter values. I want to remove hard coded values and replace it with a parameter that I can pass. This is oracle pl/sql anonymous blocks. so the code is similiar to this

function myfunc 
(p_myvar  in varchar2)
return is
 
 myvar2 varchar2(100) := 'HELLO';
end;

I want to do 2 things.

  1. change the line with p_myvar to
p_myvar in varchar2, p_myvar2 in varchar2)

and change

myvar2 varchar2(100) := 'HELLO';

to

myvar2 := p_myvar2;

This is code I am cleaning up. It is just alot of files to change. I am hope I can do this quickly with SED or something like that and its easier than doing this all manually.

I would suggest creating a temp directory and copying a few files there before running the script below. (Just to make sure it does what you need it to do)

#!/bin/ksh

cd your_directory

for file in `ls`
do
  cat $file | sed 's/(p_myvar in varchar2)/(p_myvar in varchar2, p_myvar2 in varchar2)/g' > $file.tmp
  cat $file.tmp | sed 's/myvar2 varchar2(100) := \'HELLO\';/myvar2 := p_myvar2;/g' > $file
done

One other note: Run this from a different directory from where your files are, else it might try to modify itself.