# Access Awk Variables Outside Scope

**URL:** <https://community.unix.com/t/access-awk-variables-outside-scope/166355>\
**Category:** Shell Programming and Scripting\
**Created:** [October 26, 2006, 5:25am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355 "2006-10-26T05:25:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amruta\_Pitkar](https://community.unix.com/letter_avatar/amruta_pitkar/32/6_449c533f586939b6e5f246632a46421f.png) [@Amruta\_Pitkar](https://community.unix.com/u/Amruta_Pitkar)\
**Post date:** [October 26, 2006, 5:25am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/1 "2006-10-26T05:25:58Z")

</div>

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 :

```nohighlight
/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

---

<div class="post-metadata">

**Author:** ![mahendramahendr](https://community.unix.com/letter_avatar/mahendramahendr/32/6_449c533f586939b6e5f246632a46421f.png) [@mahendramahendr](https://community.unix.com/u/mahendramahendr)\
**Post date:** [October 26, 2006, 6:00am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/2 "2006-10-26T06:00:08Z")

</div>

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

```nohighlight
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);  
    }

```

```nohighlight
#/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

```

---

<div class="post-metadata">

**Author:** ![jim\_mcnamara](https://community.unix.com/letter_avatar/jim_mcnamara/32/6_449c533f586939b6e5f246632a46421f.png) [@jim\_mcnamara](https://community.unix.com/u/jim_mcnamara)\
**Post date:** [October 26, 2006, 7:16am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/3 "2006-10-26T07:16:55Z")

</div>

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

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

```

in your shell script

```nohighlight
chmod +x ./temp.sh
. ./temp.sh

```

This will "import" three variables: FAIL SUCCESS TOTAL

---

<div class="post-metadata">

**Author:** ![mahendramahendr](https://community.unix.com/letter_avatar/mahendramahendr/32/6_449c533f586939b6e5f246632a46421f.png) [@mahendramahendr](https://community.unix.com/u/mahendramahendr)\
**Post date:** [October 26, 2006, 7:39am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/4 "2006-10-26T07:39:39Z")

</div>

Great... I got an enhancement to it

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

```

then call the awk script in shell script

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

eval $str

echo $FAIL
echo $SUCCESS
echo $TOTAL

```

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [October 26, 2006, 10:06am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/5 "2006-10-26T10:06:18Z")

</div>

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![mahendramahendr](https://community.unix.com/letter_avatar/mahendramahendr/32/6_449c533f586939b6e5f246632a46421f.png) [@mahendramahendr](https://community.unix.com/u/mahendramahendr)\
**Post date:** [October 26, 2006, 10:13am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/6 "2006-10-26T10:13:57Z")

</div>

Anbu23

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

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [October 26, 2006, 10:33am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/7 "2006-10-26T10:33:34Z")

</div>

> [@mahendramahendr](#):
>
> Anbu23
> 
> The above mentioned stuff does the samething as what you posted... seems like you have searched whole forum and posted the same 🙂

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.

---

<div class="post-metadata">

**Author:** ![najaf\_zaidi](https://community.unix.com/letter_avatar/najaf_zaidi/32/6_449c533f586939b6e5f246632a46421f.png) [@najaf\_zaidi](https://community.unix.com/u/najaf_zaidi)\
**Post date:** [January 15, 2008, 8:17am UTC](https://community.unix.com/t/access-awk-variables-outside-scope/166355/8 "2008-01-15T08:17:34Z")

</div>

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
