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 > dynamic array

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-27-03, 06:31
sudham sudham is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
dynamic array

Hi,

Everyday I get 1 or more flat files, it is sure that I get one file but it can be more, I want to sql load all the files into a table. I know how to do this for one file but for multiple files I should be able to do that in a loop. Could you please tell me how to do this in shell script? ie. How to check how many files are there in particular and access them?

Thanks in advance.

Sudha
Reply With Quote
  #2 (permalink)  
Old 08-27-03, 11:46
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Lightbulb

Try this:

#!/bin/ksh

dir='/home/myfiledir'
bkpdir='/home/mybkpdir'
LOAD_LOG=/tmp/mylog.log

echo "There are $(ls -1 $dir|wc -l) files in $dir"

for fil in $(ls -1 $dir)
do
echo "Loading $fil ..."
sqlldr / control=myfile.ctl DATA=$fil LOG=$LOAD_LOG
if [[ "$(grep ^ORA- $LOAD_LOG | wc -l)" -gt 0 ]]
then
echo "Oracle error(s) occured, check log $LOAD_LOG"
exit 3
fi
REC_CNT=$(grep ' Rows successfully loaded.' $LOAD_LOG|awk '{print $1;}')
BAD_REC=$(grep 'Total logical records rejected:' $LOAD_LOG|awk -F: '{print $2;}')
DSC_REC=$(grep 'Total logical records discarded:' $LOAD_LOG|awk -F: '{print $2;}')
REC_CNT=${REC_CNT:=0}
if test $REC_CNT -eq 0
then
echo "!Error: SQL LOADER FAILED, No records loaded."
exit 3
fi
ERR_CNT=$(expr $BAD_REC + $DSC_REC )
if [ ${ERR_CNT:-1} -ne 0 ]
then
echo "!Error: SQL LOADER FAILED, Bad or Discarded records."
exit 3
fi
echo "$REC_CNT Rows successfully loaded."

mv $fil $mybkupdir

done

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #3 (permalink)  
Old 08-28-03, 04:26
sudham sudham is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
Thanks LKBrwn_DBA,


I want to count number of files of particular pattern say x*.txt. Could you please tell me how I do that? ls -1 $dir|wc gives no of files in the dir, right?

Sorry I should have been clear.

Thanks in advance,

Sudha
Reply With Quote
  #4 (permalink)  
Old 08-28-03, 04:49
sudham sudham is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
Quote:
Originally posted by LKBrwn_DBA
Try this:

#!/bin/ksh

dir='/home/myfiledir'
bkpdir='/home/mybkpdir'
LOAD_LOG=/tmp/mylog.log

echo "There are $(ls -1 $dir|wc -l) files in $dir"

for fil in $(ls -1 $dir)
do
echo "Loading $fil ..."
sqlldr / control=myfile.ctl DATA=$fil LOG=$LOAD_LOG
if [[ "$(grep ^ORA- $LOAD_LOG | wc -l)" -gt 0 ]]
then
echo "Oracle error(s) occured, check log $LOAD_LOG"
exit 3
fi
REC_CNT=$(grep ' Rows successfully loaded.' $LOAD_LOG|awk '{print $1;}')
BAD_REC=$(grep 'Total logical records rejected:' $LOAD_LOG|awk -F: '{print $2;}')
DSC_REC=$(grep 'Total logical records discarded:' $LOAD_LOG|awk -F: '{print $2;}')
REC_CNT=${REC_CNT:=0}
if test $REC_CNT -eq 0
then
echo "!Error: SQL LOADER FAILED, No records loaded."
exit 3
fi
ERR_CNT=$(expr $BAD_REC + $DSC_REC )
if [ ${ERR_CNT:-1} -ne 0 ]
then
echo "!Error: SQL LOADER FAILED, Bad or Discarded records."
exit 3
fi
echo "$REC_CNT Rows successfully loaded."

mv $fil $mybkupdir

done


Last edited by sudham; 08-28-03 at 05:10.
Reply With Quote
  #5 (permalink)  
Old 08-28-03, 05:12
sudham sudham is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
Hi,

I sorted that ls -1 $test*.txt |wc gave me what I wanted.

Sudha


Quote:
Originally posted by sudham
Thanks LKBrwn_DBA,


I want to count number of files of particular pattern say x*.txt. Could you please tell me how I do that? ls -1 $dir|wc gives no of files in the dir, right?

Sorry I should have been clear.

Thanks in advance,

Sudha
Reply With Quote
  #6 (permalink)  
Old 09-01-03, 06:44
sudham sudham is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
Hi LRBrwn_DBA,

i am getting these errors

There are $(ls -1 /dist/scripts/testdir/|wc -l) files in /dist/scripts/testdir/
instead of
There are 20 files in /dist/scripts/testdir/
and
tt.ksh: syntax error at line 4: `$' unexpected where
line 4 is
for fil in $(ls -1 $dir)

could you please tell me why this is happening

Thanks in Advance

Sudha
Reply With Quote
  #7 (permalink)  
Old 09-01-03, 09:21
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Question

Maybe $(...) doesn't work inside quotes?

Try this:

echo "There are `ls -1 $dir|wc -l` files in $dir"

Where '`' is the back quote.

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
  #8 (permalink)  
Old 09-01-03, 10:14
sudham sudham is offline
Registered User
 
Join Date: Aug 2003
Posts: 7
Hi LKBrwn_DBA,

Thanks, i will try that...

Sudha
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