[comp.unix.questions] How to pass shell variables to awk?

pjs@euclid.jpl.nasa.gov (Peter Scott) (04/09/91)

Maybe this one should be in the FAQ; I know it came through here
several months ago, but I forgot the answer until, of course, I
wanted to do it myself.

Now I have a workaround, so I'm interested in the answer only
for academic reasons (until I need to do it again...); if I have
some (csh) variable foo and I want awk to recognize it, e.g.
awk file '{print $1-?$foo?}', what would I use instead of ? and ? ?
Thanks!


-- 
This is news.  This is your       |    Peter Scott, NASA/JPL/Caltech
brain on news.  Any questions?    |    (pjs@euclid.jpl.nasa.gov)

jik@athena.mit.edu (Jonathan I. Kamens) (04/09/91)

See posting <27852@neptune.inf.ethz.ch>, wich subject "SUMMARY: using sh vars
in awk calls.", posted on April 5 in comp.unix.shell.

There is almost no difference between using sh variables in awk and using csh
variables in awk, so that posting should answer your question.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

rhartman@thestepchild.sgi.com (Robert Hartman) (04/10/91)

In article <1991Apr8.223119.2318@elroy.jpl.nasa.gov> pjs@euclid.jpl.nasa.gov writes:
>Maybe this one should be in the FAQ; I know it came through here
>several months ago, but I forgot the answer until, of course, I
>wanted to do it myself.
>
> ... if I have
>some (csh) variable foo and I want awk to recognize it, e.g.
>awk file '{print $1-?$foo?}', what would I use instead of ? and ? ?

Use single quotes:

	awk '{... '$foo'...}' file
                  ^    ^
Since there is no white space between the quoted expressions and the (now
unquoted) shell var, the shell treates the whole program argument as just
one word.  Of course, you can't have white space in the variable value here.
If you need to guarantee against white space, use:

	awk '{... '"$foo"'...}' file

since variable expansion takes place within double quotes.

-r


ps.  I think this does qualify as an FAQ.  Second time I've answered it this
month.  (Once in c.u.shells.)

harrison@necssd.NEC.COM (Mark Harrison) (04/10/91)

In article <1991Apr8.223119.2318@elroy.jpl.nasa.gov>,
pjs@euclid.jpl.nasa.gov (Peter Scott) writes:

> Maybe this one should be in the FAQ;

I will submit the following text to the FAQ list...
Question:  How do I submit a FAQ to the FAQ list? :-)

#
# This shell script provides several answers to the question
#          "How can I pass shell variables to awk?"
#

foo=123
bar=456
foostr=hello
barstr=world
twowords="hello world"

# 1.  Surround the environment variables with single quotes.  This
#     causes awk's first argument to be a single string, composed
#     of quoted strings interspersed with values of shell variables.
#     They are treated as one argument because there are no unquoted
#     spaces.

awk 'BEGIN {print 1, '$foo' + '$bar'}' </dev/null
#   ^^^^^^^^^^^^^^^^^^%%%%^^^^^%%%%^^^
#    ^                 ^   ^    ^   ^
#    |                 |   |    |   |
#    |                 +---|----+---|------------ substituted vars
#    +---------------------+--------+------------ quoted strings

# 2.  Set an internal awk variable on the command line.  While this
#     is documented in "old" awk, on my system it only seems to work
#     with "new" awk (nawk).

nawk 'BEGIN {print 2, foo + bar}' foo=$foo bar=$bar </dev/null
awk 'BEGIN {print 3, foo + bar}' foo=$foo bar=$bar </dev/null

# 3.  To use the environment variable as a string, It must have
#     double quotes around the single quotes.  If it does not,
#     then the text of the environment variable is treated as
#     an awk variable instead of as a constant.
#
# wrong: translates to {print hello, world}, printing the values
#        of the variables _hello_ and _world_ (both zero)
#
awk 'BEGIN {print 4, '$foostr', '$barstr'}' </dev/null
#
# right: shell vars are quoted, making them strings
#
awk 'BEGIN {print 5, "'$foostr'", "'$barstr'"}' </dev/null

# 4.  You will have problems if your environment variable
#     contains spaces.  The spaces embedded in the concatenated
#     string will cause awk to only catch the first part of
#     the command string, probably resulting in a syntax error.
#     If you set the awk variable on the command line, be sure
#     to enclose it in quotes.

# Syntax error:
awk 'BEGIN {print 6, "'$twowords'"}' </dev/null
# OK:
nawk 'BEGIN {print 7, twowords}' twowords="$twowords" </dev/null
-- 
Mark Harrison             harrison@ssd.dl.nec.com
(214)518-5050             {necntc, cs.utexas.edu}!necssd!harrison
standard disclaimers apply...