really stuck- need to get a variable within a variable- AWK

Hi all,

I have been struggling with this all day, and it is key to a conversion database I have to write.

The data converts the information out of an array using AWK, and basically all I have to do is figure out how to get the value of a variable inside a variable.

Right now at its simplest level, I have:

mpgw=13
CATEGORY=mpgw

I need to get the value 13 out of CATEGORY.

I have seen documentation that says you can do (within AWK):

findCATEGORY=eval echo CATEGORY

but this is just the same as:

findCATEGORY=CATEGORY

it is always mpgw, never 13. Does this make sense?
Perhaps it can be done within a function.
I would like to stay with AWK, or I have to start this conversion program completely over from scratch, and I've been working on it for days. :confused::(:mad:

Thanks for any advice,

jeffpas

PS Perhaps a system call, writing and reading to an outside file, using a printf statement.............these are all things I'm investigating. But it has to be within the data stream (awk) so that it does it to every line being converted. No luck........

YMMV - dealing with multiple 'evals':

mpgw=13
CATEGORY=mpgw
Fred=CATEGORY

nawk -f jeff.awk myFile

jeff.awk:

BEGIN {
  FS=OFS="="
}
{ val[$1]=$2 }
{
  i=$2
  while (1) {
    if (i in val)
      i=val
    else
      break
  }
}
$2=i

This looks like it could be the key to what I'm looking for, but it appears to be piping awk through a second file. I'm not quite understanding what you mean by your example.

Is there any way to do this with a function, within the AWK program?

I already have a conversion program written, it is quite lengthy, on an AIX box. I could replace the awk call with nawk, if I type nawk on the command line, it simply says awk. There is no man page available on this system. So, not sure if I have nawk or not.

Sorry...every brain cell available already firing. Not enough left over to wrap around this............

My examples is. well...... just an example -given a sample file you provided - how to convert it to what (I think) is needed.

Not having a complete data set nor a complete awk script - it's hard to say how to adjust a sample code above to your particular circumstance. You probably know all the details of what your awk script is doing the best - so you'll have massage the given code sample.

But...

BEGIN {
   FS=OFS="="
}
# the rest of the script


function evalVars(   i)
{
   val[$1]=$2
   i=$2
   while (1) {
      if (i in val)
        i=val
      else
        break
    }
    $2=i
}

# some more of the script
#......

# some action here
{ 
   # resolve the vals
   evalVars()
  # do more stuff with the "resolved" data
  print
}

vgersh99,

I assume by your example you mean (for example):
in code:

>>
function recurse(i)
{
val[$1]=$2
i=$2
while (1) {
if ( i in val )
i = val
[i]else
break
}

               $2=i
  return i

}

Then further down in program, to get the value:

find_LOGCAT=recurse(LOGCAT)

>>>

So for example if you have (all in AWK):

mpgw=13 (this is a log category)
LOGCAT=mpgw (current category)

and want to know, what is the value of the current category?

in code, should be:
>>
find_LOGCAT=recurse(LOGCAT)
>>

Unfortunately, instead of pulling up 13 for every record that has 'mpgw', the awk output for LOGCAT is 14 and sometimes 15.........very odd.

Sorry, I indented everything, the forum seems to have removed all the formatting.
I am completely baffled by $1 and $2, don't understand this logic, but it appears to be at least pulling numbers at this point. Odd that I have no code 15, and that it isn't always the same number. But it should be 13.
not sure what the problem is.

hi,
look at the shell variables for the answer

try this link: http://sysunconfig.net/aixtips/ksh_tips.txt

Look at this:

====================

#!/bin/ksh

mpgw=13
CATEGORY=mpgw

CATEGORY2=$mpgw

echo $mpgw
echo $CATEGORY

echo $CATEGORY2

Hope this help

It looks like I also have 'gawk' capability, and can use this instead.
Not sure what the difference is. Seems basically the same.

Unfortunately I am trapped because it looks as if the shell variables aren't available within an AWK script.

$LOGCAT becomes $0, which just kicks up the entire line instead of a value.

Looks like I may have sunk my own ship by going with AWK.
So close to being finished, and yet the whole day on this one thing...........who would have thought AWK couldn't do a variable in a variable, yet UNIX can.

I guess the writers of AWK have been my downfall......:frowning:

once again.... You've been provided A sample way of how to solve your initial sample task. All you need to do it understand the code and adjust it your own requirements and/or awk code.

what is this forum, Jeopardy?????

Can anyone just give a plain answer, if they know how to do this.
I would be more than willing to do the same. My job is at stake here.

If you honestly don't know how to do this, but were just trying to help, then thanks for your advice.
As for me it looks to me like I'm going to have to convert this over to Perl or some other language- its going to be a long night.

Folks, it looks like AIX has a special command called 'a2p', which will actually convert an AWK script on the fly into a functioning PERL program.

I took the ksh pipe out of the 'convert' script, then ran the convert script with

a2p convert > pervert
perl pervert test.log

Appears to run perfectly. I'll be damned, I'm impressed.

I may try my luck with another language here-
If anyone nails the answer to this thread, feel free to post.

YouTube - Intro the jeffersons

The very simple way to do this is to use the "eval" statement. What does "eval" do?

When the shell evaluates (=executes) a command line it does so in several stages. One stage is to "expand" the variables, meaning: replace the variables name by its content. For instance:

x="abc"
print - "$x"

The first step the shell takes is to replace the "$x" with its content. The line after this operation is:

print - "abc"

Only then the command is executed. "eval" restarts this process of expanding variables after it has already taken place (and before the execution of commands). For instance, the steps in "slow-motion":

x="abc"
abc="This is the content of abc."

eval print - \$$x

eval print - $abc                     # after first expansion: "\$" -> "$", "$x" expanded
print - This is the content of abc.   # after eval: "$abc" expanded

That works the same way with arrays. The reason why you have to escape the "$" by backslashes is to protect them from being interpreted in the first run of the shell interpreter: otherwise the shell would read "$$" (which is a perfect shell variable, containing the PID) followed by "x", which is simply a character.

Coming back to your problem: If you want to get out "13" as result the line to execute is - not within awk, but on the command line or within a shell script:

mpgw=13
CATEGORY=mpgw

eval print - "\$CATEGORY"

Suppose you have several values and you want to select one:

mpgw=13
foo=14
bar=15

CATEGORY="mpgw" ; eval print - "\$$CATEGORY"     # will yield "13"

CATEGORY="foo" ; eval print - "\$$CATEGORY"      # will yield "14"

At last a remark as the moderator here: you might not be "damned", but you might be left alone and/or receive an infraction if you don't tone down your comments. All the people posting in this thread - especially vgersh99 - have been genuinely and sincerely trying to help you, so you have nothing to complain about.

It might be troublesome for *you* that your job is at stake, as you put it, but it is not our fault that you have a job for which you are lacking the necessary qualifications. We are generally inclined to help you along (like everybody else coming here), but first: we are volunteers, therefore rather "inclined" than "bound by duty" to do so; and second: it is *your* problem not to know something you should know, not *ours* that you might not understand our offerings.

bakunin