|> Is there any way to embed sh environment variables in an awk script??
Yes. At least two different ways, in fact.
|> eg. user=me;awk '{ print $1, ${user} }' .rhosts
This doesn't work, simply because shell variable substitution does not take
place inside single quotes. That is, after all, *why* you are using the
single quotes -- to prevent the '$' character from being interpreted as the
sign of an impending shell variable name, so that you can use it in awk
expressions.
The command above would work if you did:
user=me; awk '{print $1, '"$user"' }' .rhosts
The double quotes around $user are not strictly necessary, because it's
probably going to be only one word long and not contain any special shell
characters, but I put them there because you may at some point may want to
feed a variable with spaces and/or special characters into awk.
The other way to pass values in is to use a often undocumented feature of awk:
awk '{print $1, user }' user="$user" .rhosts
In addition to being undocumented, this feature is sometimes inconsistent on
different versions of awk -- some versions will define the values you specify
before the BEGIN block is executed, and some won't define the values until
after BEGIN.
Note, further, that when you specify a variable setting as a "fake file name"
as I've indicated above, awk (or, at least, some versions of it) won't
automatically read from stdin if no real files are specified, so you need to
add a "-" argument to the end of your command if you want awk to read stdin.
--
Jonathan Kamens USnail:
MIT Project Athena 11 Ashford Terrace
Office: 617-253-8085 Home: 617-782-0710