Replace patterns in a file
#!/bin/bash
# February 3th, 2018
# Create a table copy in the same database
#
shPid=$$
inputSqlFile=statement.sql
tmp=".sql.${shPid}"
newInputSqlFile=statement_$shPid.sql
pattern="alter table"
target_tableName="tmp.people_copy"
source_tableName="tb01.people"
input_tablespace="ss"
tablespaceName="in $input_tablespace"
if [ -z "$input_tablespace" ] ; then
tablespaceName=""
fi
rTarget_table="target_table"
rSourve_table="source_table"
rTablespace="in_tablespace"
echo "$inputSqlFile"
sed "/$pattern/Q" $inputSqlFile > $tmp
cp $inputSqlFile $tmp
sed "s/${rTarget_table}/${target_tableName}/" $tmp > $newInputSqlFile
sed "s/${rSourve_table}/${source_tableName}/" $newInputSqlFile > $tmp
sed "s/${rTablespace}/${tablespaceName}/" $tmp > $newInputSqlFile
cat $newInputSqlFile
rm $tmp
rm $newInputSqlFile
--------------------------------------
statement.sql
--------------------------------------
-- Copy a table in the same database
drop table target_table;
create table target_table like source_table in_tablespace;
commit;
alter table target_table ...;
commit;
insert into target_table ... from source_table;
commit;
# February 3th, 2018
# Create a table copy in the same database
#
shPid=$$
inputSqlFile=statement.sql
tmp=".sql.${shPid}"
newInputSqlFile=statement_$shPid.sql
pattern="alter table"
target_tableName="tmp.people_copy"
source_tableName="tb01.people"
input_tablespace="ss"
tablespaceName="in $input_tablespace"
if [ -z "$input_tablespace" ] ; then
tablespaceName=""
fi
rTarget_table="target_table"
rSourve_table="source_table"
rTablespace="in_tablespace"
echo "$inputSqlFile"
sed "/$pattern/Q" $inputSqlFile > $tmp
cp $inputSqlFile $tmp
sed "s/${rTarget_table}/${target_tableName}/" $tmp > $newInputSqlFile
sed "s/${rSourve_table}/${source_tableName}/" $newInputSqlFile > $tmp
sed "s/${rTablespace}/${tablespaceName}/" $tmp > $newInputSqlFile
cat $newInputSqlFile
rm $tmp
rm $newInputSqlFile
--------------------------------------
statement.sql
--------------------------------------
-- Copy a table in the same database
drop table target_table;
create table target_table like source_table in_tablespace;
commit;
alter table target_table ...;
commit;
insert into target_table ... from source_table;
commit;
Comentarios