操作数据库的shell
时间:2009-08-15 来源:w1_xiao
myawk()
{
awk -F '|' ' BEGIN{ flag = 0; fnum = 0; dataflag = 0; beginflag = 0; endflag = 0;}
{
if( $0 ~/^SQL>/ && beginflag == 0 )
beginflag = 1;
if( $0 ~/^SQL> Disconnect/ )
exit;
if( beginflag == 0 )
next;
if( $0 ~/^ *$/ )
next;
# if( $0 ~/^SQL>.*SQL>.*SQL>.*SQL>/ )
# next;
if( dataflag == 1 ) {
for( i=1; i< fnum+1; i ++ ) {
datalen[i] = length( $i );
}
for( i=1; i< fnum+1; i ++ ) {
fmt = sprintf( "%%-%d.%ds|", datalen[i], datalen[i] );
printf( fmt, i );
if( i == fnum )
printf( "\n" );
}
printf( "---------------------------------------------------------------------\n" );
}
if( $0 ~ /|/ && flag == 0 ) {
if( NF > 5 ) {
fnum = NF;
for( i=1; i< fnum+1; i ++ ) {
namelen[i] = length( $i );
if( i == fnum )
printf( "\n" );
}
printf( "\n" );
}
flag = 1;
}
dataflag = 0;
if( $0 ~/--|-\|/ ) {
for( i=1; i< fnum+1; i ++ ) {
fmt = sprintf( "%%-%d.%ds|", namelen[i], namelen[i] );
printf( fmt, i );
if( i == fnum )
printf( "\n" );
}
dataflag = 1;
next;
next;
}
print $0;
}' $1
}
sql_ora()
{ sqlplus name/passwd@ordb <<!
--spo /tmp/sqltmp.spo cre
set feedback 1
set pagesize 30
set linesize 2500
set colsep '|';
set numwidth 12;
set trimout on;
@/tmp/sqltmp.sql
quit
! }
if [ -f $1 ]
then
cp $1 /tmp/sqltmp.sql
else echo ""
echo "$1" | sed 's/ *$//' |sed '/[^;]$/s/$/;/' > /tmp/sqltmp.sql
fi sql_ora >/tmp/sqltmp.out
#cat /tmp/sqltmp.out | sed 's/ / /g; s/ */ /g' |myawk
#cat /tmp/sqltmp.out | sed 's/ / /g' |myawk
cat /tmp/sqltmp.out | sed 's/[ ][ ]*/ /g; /^[ -][ -]*$/d;' |myawk
echo "" #rm -f /tmp/sqltmp.sql 2>/dev/null
#rm -f /tmp/sqltmp.out 2>/dev/null
{
awk -F '|' ' BEGIN{ flag = 0; fnum = 0; dataflag = 0; beginflag = 0; endflag = 0;}
{
if( $0 ~/^SQL>/ && beginflag == 0 )
beginflag = 1;
if( $0 ~/^SQL> Disconnect/ )
exit;
if( beginflag == 0 )
next;
if( $0 ~/^ *$/ )
next;
# if( $0 ~/^SQL>.*SQL>.*SQL>.*SQL>/ )
# next;
if( dataflag == 1 ) {
for( i=1; i< fnum+1; i ++ ) {
datalen[i] = length( $i );
}
for( i=1; i< fnum+1; i ++ ) {
fmt = sprintf( "%%-%d.%ds|", datalen[i], datalen[i] );
printf( fmt, i );
if( i == fnum )
printf( "\n" );
}
printf( "---------------------------------------------------------------------\n" );
}
if( $0 ~ /|/ && flag == 0 ) {
if( NF > 5 ) {
fnum = NF;
for( i=1; i< fnum+1; i ++ ) {
namelen[i] = length( $i );
if( i == fnum )
printf( "\n" );
}
printf( "\n" );
}
flag = 1;
}
dataflag = 0;
if( $0 ~/--|-\|/ ) {
for( i=1; i< fnum+1; i ++ ) {
fmt = sprintf( "%%-%d.%ds|", namelen[i], namelen[i] );
printf( fmt, i );
if( i == fnum )
printf( "\n" );
}
dataflag = 1;
next;
next;
}
print $0;
}' $1
}
sql_ora()
{ sqlplus name/passwd@ordb <<!
--spo /tmp/sqltmp.spo cre
set feedback 1
set pagesize 30
set linesize 2500
set colsep '|';
set numwidth 12;
set trimout on;
@/tmp/sqltmp.sql
quit
! }
if [ -f $1 ]
then
cp $1 /tmp/sqltmp.sql
else echo ""
echo "$1" | sed 's/ *$//' |sed '/[^;]$/s/$/;/' > /tmp/sqltmp.sql
fi sql_ora >/tmp/sqltmp.out
#cat /tmp/sqltmp.out | sed 's/ / /g; s/ */ /g' |myawk
#cat /tmp/sqltmp.out | sed 's/ / /g' |myawk
cat /tmp/sqltmp.out | sed 's/[ ][ ]*/ /g; /^[ -][ -]*$/d;' |myawk
echo "" #rm -f /tmp/sqltmp.sql 2>/dev/null
#rm -f /tmp/sqltmp.out 2>/dev/null
相关阅读 更多 +