awk supports this:
awk '{print $(NF-1);}'
but not for user-defined variables:
awk '{a=123; b="a"; print $($b);}'
by the way, shell supports this:
a=123;
b="a";
eval echo \${$b};
How can I achieve my purpose in awk?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Not at the moment. However, if you provide a wrapper, it is (somewhat hacky and dirty) possible.
The idea is to use @ operator, introduced in the recent versions of gawk.
This @ operator is normally used to call a function by name.
So if you had
Now, this is usefull on it’s own.
Assuming we know var names beforehand, we can write a wrapper to indirectly modify and obtain vars
So now, for each var name you want to prrvide access by name, you declare these two functions
And prahaps, somewhere in your BEGIN{} block,
To declare it for global access.
Then you can use
This may appear useless at first, however there are perfectly good use cases, such as
keeping a linked list of vars, by holding the var’s name in another var’s array, and such.
It works for arrays too, and to be honest, I use this for nesting and linking Arrays within
Arrays, so I can walk through multiple Arrays like using pointers.
You can also write configure scripts that refer to var names inside awk this way,
in effect having a interpreter-inside-a-interpreter type of things, too…
Not the best way to do things, however, it gets the job done, and I do not have to worry about
null pointer exceptions, or GC and such 🙂