Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:58:57+00:00 2026-06-09T14:58:57+00:00

awk supports this: awk ‘{print $(NF-1);}’ but not for user-defined variables: awk ‘{a=123; b=a;

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:58:58+00:00Added an answer on June 9, 2026 at 2:58 pm

    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

    function foo(s){print "Called foo "s}
    function bar(s){print "Called bar "s}
    {
        var = "";
        if(today_i_feel_like_calling_foo){
            var = "foo";
        }else{
            var = "bar";
        }
        @var( "arg" ); # This calls function foo(), or function bar() with "arg"
    }
    

    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

    function get(varname, this, call){call="get_"varname;return @call();}
    function set(varname, arg, this, call){call="set_"varname; @call(arg);}
    

    So now, for each var name you want to prrvide access by name, you declare these two functions

    function get_my_var(){return my_var;}
    function set_my_var(arg){my_var = arg;}
    

    And prahaps, somewhere in your BEGIN{} block,

    BEGIN{ my_var = ""; }
    

    To declare it for global access.
    Then you can use

    get("my_var");
    set("my_var", "whatever");
    

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

awk: Function systime is not defined. but systime is a built in command
This works perfectly: awk '{for (i=1;i<=NF;i++) if($i ~/mystring/) print $1, $i}' But I also
If I run this on OS X: last -10 | awk '{print $1}' I
The command awk 'END{print NR}' file.log returns an integer, but with a space after
I have an awk script that I have defined thus: #!/usr/bin/env awk BEGIN {
when I call my awk script, I keep getting an error : sam@sam-laptop:~/shell/td4$ awk
awk '/^nameserver/ && !modif { printf(nameserver 127.0.0.1\n); modif=1 } {print}' testfile.txt It is displaying
awk newbie here.. I'm trying this: top -b -p 30259 | awk 'BEGIN {
I have this awk script: #!/usr/bin/awk -f BEGIN {IGNORECASE=1} /nVidia/ {inner=1;pci=$1} /(corr|fatal)err\-/ {if (inner)
I am on AIX system. But grep on AIX does not support -B ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.