Exporting Awk Variables?

John Mundt john at chinet.chi.il.us
Thu May 30 02:38:56 AEST 1991


In article <4255 at polari.UUCP> gcs at polari.UUCP (Greg Sheppard) writes:
>
> This is probably a repeat question but the thread scrolled by (at
>least I think I remember something along the same lines awhile back).
>Is it possible to export a variable within awk so the variable
>could be used by a shell script?  I have an awk script which
>increments a variable "i".  When awk is finished I'd like to pass
>the value of "i" to the same shell script which calls awk.
> Email responses would be fine or if someone could point me to
>a location where comp.unix.shell is archived...I can try to look
>it up.  Thanks!

You can wrap the awk script in backslashes and have it write
the output of i as it exits to stdout and assign it to a variable
in the calling script, as in

....
RET_VAR=`awk ' { do_somthing; ...}; END { printf("%d",i); }'`
...

RET_VAR will hold the value of i.  You would proably have to
use awk's redirect functions to send other useful output to a
file.  Alternatively, you could use the END statement to print
the value to a temp file, and then read that number into 
RET_VAR.
-- 
---------------------
john at admctr.chi.il.us
John Mundt   Teachers' Aide, Inc.  P.O. Box 1666,  Highland Park, IL
(708) 998-5007 || -432-8860 



More information about the Comp.unix.shell mailing list