Hello All:
We are looking to search through 2000 files with around 21,000 statements where we have to search, replace and insert a pattern based on the following:
1) Parse through the file and check for CREATE MULTISET TABLE or CREATE SET TABLE statements.....and they always end with ON COMMIT PRESERVE ROWS;
CREATE MULTISET VOLATILE TABLE vt_test
, NO FALLBACK, NO JOURNAL, NO LOG AS
(
select col1,max(col2) as col2
from vt_test
where col3 in (1,2,3)
)
WITH DATA
PRIMARY INDEX (col1)
ON COMMIT PRESERVE ROWS;
2) Replace WITH DATA to WITH NO DATA. If there is already NO DATA, skip changing it.
CREATE MULTISET VOLATILE TABLE vt_test
, NO FALLBACK, NO JOURNAL, NO LOG AS
(
select col1,max(col2) as col2
from vt_test
where col3 in (1,2,3)
)
WITH NO DATA
PRIMARY INDEX (col1)
ON COMMIT PRESERVE ROWS;
3) Add an INSERT statement right after this....with the same table name. Basically, take the SELECT part from above and end it with a semi-colon. The challenge is code is not formatted. It can be in one line...and can be a mix of CAPS and SMALL (case insensitive). Final code should look like this:
CREATE MULTISET VOLATILE TABLE vt_test
, NO FALLBACK, NO JOURNAL, NO LOG AS
(
select col1,max(col2) as col2
from vt_test
where col3 in (1,2,3)
)
WITH NO DATA
PRIMARY INDEX (col1)
ON COMMIT PRESERVE ROWS;
INSERT INTO vt_test
select col1,max(col2) as col2
from vt_test
where col3 in (1,2,3);
4) Output a new file in another directory....
I am comfortable doing the pattern replace using awk, but not well versed with doing the additional step. Please see below and see if you can help.
#!/usr/bin/ksh
#|------------------------------------------------------------------|
#| Split the CREATE TABLE AS into DDL and DML Step
#|------------------------------------------------------------------|
usage ()
{
echo " Usage: $0 <SRC_DIR> <TGT_DIR>"
}
if [ $# -lt 2 ]; then
usage
exit;
fi
SRC_DIR=$1
TGT_DIR=$2
for i in *.prc
do
awk 'BEGIN{IGNORECASE=1} {gsub(/WITH DATA/,"WITH NO DATA");print}' $i > TGT_DIR
done