unix awk scripting

Hi
can anyone pls help me out in knowing how a array of elements which are fetched from database is passed to the awk program.

Something like this

#!/bin/sh

AMT[]=`sqlplus -s usrname/pswd <<EOD
set pagesize 0 feedback off verify off heading off echo on
select amount from tablename where condition;
EOD`

noofrec=`sqlplus -s usrname/pswd <<EOD
set pagesize 0 feedback off verify off heading off echo on
select count(*) from tablename where condition;
EOD`

echo "$AMT[]" $noofrec | awk '
BEGIN
{
new=$1;
printf "%d" ,new; //this works fine with 2nd argumnet but dont know how
printf the arrays values?
}

{
printf "%d" ,AMT???
}'END

Hi,

I am not sure why you will process like this, why not spool the sql result to a file and then process the file. This seems easy to release.

Here we can treat the spool file as the array, it should be ok for you. Just try it.

sqlplus -s ops\$global/developer@sf2a <<!
spool a.txt
select exl_name from exchange_list where rownum<=5; (your sql here)
spool off
!
while read line
do
what you want to do with the each result.
done < a.txt

thank you for the idea,

this works fine but my reqiremnt asks for more than one data to be fetched which also works but same needs to be parsed in the awk program.

My requirement says of priting the values of each elements one by one to a file that too in a loop.

regards
Mohima

How about:

echo $noofrec "${AMT[@]}"|awk '{nrec=$1;for (i=2;i<=NF;i++) {print $i;shift}}'

:slight_smile:

it works fine,

i have to print the elements of the array in the loop.:confused:

regards
Mahi

No, you don't need "the loop". xargs will do that for you.

echo $noofrec "${AMT[@]}"| xargs

well it would be bit difficult to satisfy a requirement which requires something like this

#!/bin/sh

//iam reading the data for amounts from database

AMT[]=`sqlplus -s usrname/pswd <<EOD
set pagesize 0 feedback off verify off heading off echo on
select amount from tablename where condition;
EOD`

//writing to a file created at a specifed direcotry using a awk program

echo "$AMT[]" $noofrec | awk '
BEGIN
{
new=$1;
------------------------------------------------------------------
//i need to write the contents of the array in the file (which is created
at a specified location) in a loop that prints in each line contents of amount AMT[] array(as long as elements are there).

------------------------------------------------------------------
hence my problem is to print the elements in the array one by one in lines which are equivalent to no of elements in the array.

for i furthur read this file for processing

Iam able to get the contents into the array from db but couldnt print it in the file

please need suggestions asap.

Regards
Mahima
--------------------------------------------------------------------

printf "006%d" ,new; //this works fine with 2nd argumnet but dont know how
printf the arrays values?
}

{
printf "%d" ,AMT???
}'END

Wait. Are you thinking this code reads the output from sqlplus and sends each line into an array called AMT? It doesn't. You need something like this (at least in BASH):

AMT=(`sqlplus -s usrname/pswd <<EOD >$TMPDIR/$0.$$ 
set pagesize 0 feedback off verify off heading off echo on
select amount from tablename where condition;
EOD`)

# now you have contents in an array named A.

# print it as a list
echo ${A[@]} | tr ' ' '\n'

# do stuff with each line
for value in ${A[@]}
  echo $value
done

Use xargs if you have lines of input and want to do something so that all the lines appear on one output line.

working with Ksh this works only in BASH.

I cannot help you. You are either lying or playing a game. In your first post in this thread, you used code that was to run "#!/bin/sh", which either means the real bourne shell or, in some systems, bash, and definitely not ksh. You simultaneously want to have output from a command put in a shell array and fed into awk. Finally, you make no apparent effort to help yourself.

Thank You so much!

Regards
Shaik

Thank you
Regards,