Access Awk Variables Outside Scope

My awk script searches for specified patterns in a text file and stores these values into mem variables.
Once this is done I want to Insert these values into a table.

How can I avail of the variable values outside the scope of awk script....

One method that I have tried is to write the variables values into a temp file with delimiters and then read these values from a shell script, form a connection to database and insert the values into table....But I feel this is doing things in a roundabout way....

What wud be a straighter approach ? I dont want to sacrifice the awk script !

The awk script now :

/Total logical records skipped:/ {skiprecs=$5;}
/Total logical records read:/ {readrecs=$5;}
/Total logical records rejected:/ {rejectrecs=$5;}
/Total logical records discarded:/ {discardrecs=$5}
END { totalfailrecs = skiprecs + rejectrecs + discardrecs ;
      totalsuccessrecs = readrecs ;
      totalrecs = totalfailrecs + totalsuccessrecs ;
      printf "Printing the results "
      printf ("\n")
      printf ("Failed Records : %d\n", totalfailrecs);
      printf ("Success Records : %d\n", totalsuccessrecs);
      printf ("Total Records : %d\n", totalrecs);
      printf ("%d,%d,%d",totalfailrecs,totalsuccessrecs,totalrecs)>>"tempfile"
    }

Thanks
Amruta Pitkar

I don't think we can retrieve values from awk to unix shell... when awk finishes we loose all it values... I can provide you another alternative method instead of creating and parsing it..

a1.awk

/Total logical records skipped:/ {skiprecs=$5;}
/Total logical records read:/ {readrecs=$5;}
/Total logical records rejected:/ {rejectrecs=$5;}
/Total logical records discarded:/ {discardrecs=$5}
END { totalfailrecs = skiprecs + rejectrecs + discardrecs ;
      totalsuccessrecs = readrecs ;
      totalrecs = totalfailrecs + totalsuccessrecs ;
      printf ("%d,%d",totalfailrecs,totalsuccessrecs);  
    }
#/usr/bin/ksh

oput=$(awk -f a1.awk inputfile)
totalfailrecs=cut -d"," -f1
totalsuccessrecs=cut -d"," -f2

total_recs=$(($totalfailrecs+$totalsuccessrecs))

do what ever you want after this

This is clunky but will work:
Add this one last line in the END section:

printf ("FAIL=%d\nSUCCESS=%d\nTOTAL=%d",totalfailrecs,totalsuccessrecs,totalrecs)>"temp.sh"

in your shell script

chmod +x ./temp.sh
. ./temp.sh

This will "import" three variables: FAIL SUCCESS TOTAL

Great... I got an enhancement to it

printf ("FAIL=%d;SUCCESS=%d;TOTAL=%d",totalfailrecs,totalsuccessrecs,totalrecs);

then call the awk script in shell script

str=$(awk -f test.awk inputfile)

eval $str

echo $FAIL
echo $SUCCESS
echo $TOTAL
eval `echo $* | awk -F: '{printf("a=%s; b=%s; c=%s\n", $1, $2, $3)}'`
print $a
print $b
print $c

I got this tip somewhere in this forum.

1 Like

Anbu23

The above mentioned stuff does the samething as what you posted... seems like you have searched whole forum and posted the same :slight_smile:

I got this tip when i was going through the archive two months back. But now i don't remember who posted it or the link.

code--

printf ("FAIL=%d\nSUCCESS=%d\nTOTAL=%d",totalfailrecs,totalsuccessrecs,totalrecs)>"temp.sh"

just use it like this --

printf ("FAIL=%d\nSUCCESS=%d\nTOTAL=%d",totalfailrecs,totalsuccessrecs,totalrecs) | sh

bingo! its done