Unix oracle script

Hi All ,

I am trying to get the count of the record in a table into a file or a variable .

set heading off
set feedback off
set linesize 20
set pages 0
set newpage 0
set verify off
spool test.txt

select count(*) from test_data where dfu_load_date='23/05/2012' and campaign_code='sd1';
quit;
spool off;
END
cnt=`wc -l test.txt `
echo "The records uploaded= ${cnt}"

Now from above script i am trying to get the count into a spool file and the pic the count from there.

Could someone please suggest any way from which i can get the count of the records into a variable or txt file.

Thanks,
Preetpal

Try..

wc -l test1.txt| awk '{print $1}'

Hi ,

I am getting following result in text1.txt
from which i cant fetch the row count

SQL>
SQL> select count(*) from test_data where dfu_load_date='23/05/2012' and campaign_code='sd1';
       498
SQL>

Can you post the text1.txt file content...

Hi
You can try any one of the ways mentioned here: retrieve data from sqlplus into a variable

Note: That link contains some errors though, since EOF is not at the beginning of the line in the examples. And the double quotes are wrong and cannot be cut-and-pasted...

1 Like

@scrutinizer: Thanks for pointing it out.

give a try like eh below:

$ cat s.txt
SQL> select sum(salary) from emp;

SUM(SALARY)
-----------
      30807

SQL> spool off
Jay@Jay-PC ~
$ grep  " *[0-9]" s.txt
      30807

---------- Post updated at 12:45 PM ---------- Previous update was at 12:32 PM ----------

To be precise, your text.txt contains all the lines between two spool commands.
Since you require only the digits from result set, below two lines will be suffice.

c=`grep "[0-9]" s.txt`
echo "the value is $c"

Thanks for your reply!!!
:slight_smile:

Read a value right into a shell variable. This example can process a line of results at a time.

$ cat x
#!/bin/ksh

( $ORACLE_HOME/bin/sqlplus -s login/password@database <<EOF
  set heading off;
  set pagesize 0;
  select 'count='||count(*)
  from dual;
  exit;
EOF
)|
while read line     # Read a line at a time.  Could get blank lines.
do
 if [[ -n $line ]]  # If the line read is not blank...
   then eval $line  # eval causes $line to be evaluated twice, thus
                    # setting shell variables with the data returned.

        ## Print out what was read in.  Quotes are needed around the
        ## variables so printf sees nulls.
        printf "%d\n" "$count"
 fi
done

exit 0
$ x
1
$

I would be remiss if I did not point out that if what is being eval'd could be manipulated into containing a valid shell command, it would be executed by eval. So be careful with eval.