Results 1 to 8 of 8

Thread: dynamic array

  1. #1
    Join Date
    Aug 2003
    Posts
    7

    Unanswered: 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

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

  3. #3
    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

  4. #4
    Join Date
    Aug 2003
    Posts
    7
    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 06:10.

  5. #5
    Join Date
    Aug 2003
    Posts
    7
    Hi,

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

    Sudha


    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

  6. #6
    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

  7. #7
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

  8. #8
    Join Date
    Aug 2003
    Posts
    7
    Hi LKBrwn_DBA,

    Thanks, i will try that...

    Sudha

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •