No. It is not possible to assign non-scalar variables like this on the command line. But it is not too hard to make it.
If you can accept a fixed array name:
awk -F= '
FNR==NR { a[$1]=$2; next}
{ print a[$1] }
' <(echo $'index=value\nindex two=value two') <(echo index two)
If you have a file containing the awk syntax for array definitions, you can include
it:
$ cat <<EOF >file.awk
ar["index"] = "value"
ar["index two"] = "value two"
EOF
$ gawk '@include "file.awk"; BEGIN{for (i in ar)print i, ar[i]}'
or
$ gawk --include file.awk 'BEGIN{for (i in ar)print i, ar[i]}'
If you really want, you can run gawk
with -E
rather than -f
, which leaves you with an uninterpreted command line. You can then process those command line options (if it looks like a variable assignment, assign the variable). Should you want to go that route, it might be helpful to look at ngetopt.awk.
gawk
specific one—I almost always stick to POSIX features. I think I would also useprintf
in preference toecho
. – Wildcard Jan 12 '16 at 22:20