Title: AWK, use an environment variable inside a script
Author: Sandro Tosi
Last modified: 2005-02-05
To gain this, we have to go into two steps; the first one regards the
way AWK manages its own variables. We can set some variables at the
start of the script, this way
# awk -v variable=<value> <awk_script_code>
now, ``variable'' is valorized inside the code and could be used as
usual.
What we really want to do is use an environment variable, say
``varshell'', inside our script; the easy (and also wrong way) is
this:
# varshell="some data we need"
# awk -v variable=$varshell <awk_script_code>
doing so we will get an error
awk: cannot open <element> (No such file or directory)
This is due to the presence of spaces, `` '', inside varshell. To
solve this problem, do that simple modification:
# varshell="some data we need"
# awk -v variable="$varshell" <awk_script_code>
and now we are able to execute the script, using the data inside
varshell in our AWK code.
|