If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Shell Script to Convert Table to Flat file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-23-04, 01:22
kiranpulluri kiranpulluri is offline
Registered User
 
Join Date: Apr 2003
Location: USA
Posts: 1
Shell Script to Convert Table to Flat file

Hi All,

I want to write a unix script to convert a table to a flat file with a pipe delimiter. I have 25 tables and i want to write a single shell script so that i can convert the 25 tables into 25 flat files.

i wrote the follwoing for a single table to single flat file. can anyone make this file so that it can take 25 tables to 25 flat files(differnt)

SCRIPTOUTPUT=$TMPPATH/$test.txt
cat /dev/null>$SCRIPTOUTPUT
sqlplus -s / << EOF
-- Blank Line --
-- Blank Line --
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
spool $SCRIPTOUTPUT;
set heading off
set echo off
select col1 '|' col2 '|' from table1;
exit;
EOF

thanks
kiran
Reply With Quote
  #2 (permalink)  
Old 01-23-04, 02:04
TimoV TimoV is offline
Registered User
 
Join Date: Dec 2003
Posts: 56
Re: Shell Script to Convert Table to Flat file

How about using a for loop? Something along the lines of:

Code:
SCRIPTOUTPUT=$TMPPATH/$test.txt
cat /dev/null>$SCRIPTOUTPUT
for i in tablename1 tablename2 tablename3
do
  sqlplus -s / << EOF
  -- Blank Line --
  -- Blank Line --
  whenever sqlerror exit sql.sqlcode;
  whenever oserror exit failure;
  spool $SCRIPTOUTPUT;
  set heading off
  set echo off
  select col1 '|' col2 '|' from $i;
  exit;
  EOF
done
Something along those lines is what we use at work (not there atm so can't check complete syntax/script for ya).
Reply With Quote
  #3 (permalink)  
Old 01-23-04, 11:05
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
// this is very similar to previous solution, jsut using WHILE loop

you may create a file (table_file) that has entries for all your 25 tables
and then use it while loop.



while read aTable;
do
sqlplus -s / << EOF
-- Blank Line --
-- Blank Line --
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
spool $SCRIPTOUTPUT;
set heading off
set echo off
select col1 '|' col2 '|' from $aTable;
exit;
EOF
done < table_file;
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On