My way – run sqlplus, spool in using html markup and convert this html onto text by using sed replacements. It works fast enought, and better than parse resulted text by spool without markup. Bash script which impelement it is under below

#!/bin/bash
# alexey bokov – http://bokov.net/weblog
# script converts(spool=dump) oracle table onto tab separated text file via spooling it html and convert html to text.
# strange way, but it works.
OUTPUT_TEXT_FILE=”./oracle_db.csv”
ORACLE_USER=USER
ORACLE_PASSWORD=1234
ORACLE_TABLE=MY_DATA
HTML_FILE=”./dump.html”
ORACLE_QUERY=”./req.sql”
echo “set echo off” > $ORACLE_QUERY
echo “set markup html on” >> $ORACLE_QUERY
echo “set linesize 500” >> $ORACLE_QUERY
echo “set pagesize 0” >> $ORACLE_QUERY
echo “set newpage 0” >> $ORACLE_QUERY
echo “set termout off” >> $ORACLE_QUERY
echo “spool $HTML_FILE” >> $ORACLE_QUERY
echo “select * from $ORACLE_TABLE;” >> $ORACLE_QUERY
echo “spool off” >> $ORACLE_QUERY
echo “exit;” >> $ORACLE_QUERY
echo “Spool $ORACLE_TABLE onto $HTML_FILE ( take some time )…”
sqlplus $ORACLE_USER/$ORACLE_PASSWORD @$ORACLE_QUERY
echo “Convert html onto text file ( take some time also )…”
# replace all eol
tr ‘\n’ ‘ ‘ < $HTML_FILE > $OUTPUT_TEXT_FILE
sed -i ‘s/
/\n/g’ $OUTPUT_TEXT_FILE
sed -i ‘s/<\/td>/\t/g’ $OUTPUT_TEXT_FILE
# replace all most html tags
sed -i ‘s/<[^>]*>//g’ $OUTPUT_TEXT_FILE
#replace   ” &
sed -i ‘s/ / /g’ $OUTPUT_TEXT_FILE
sed -i ‘s/”/”/g’ $OUTPUT_TEXT_FILE
sed -i ‘s/&/-/g’ $OUTPUT_TEXT_FILE
#cleanup
rm $HTML_FILE
rm $ORACLE_QUERY
echo ” Done, check out $OUTPUT_TEXT_FILE for result…”

Leave a Reply