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


 
Thread Tools Search this Thread
Operating Systems AIX really stuck- need to get a variable within a variable- AWK
# 1  
Old 07-02-2008
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. SmilieSmilieSmilie

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........

Last edited by jeffpas; 07-02-2008 at 03:27 PM..
# 2  
Old 07-02-2008
YMMV - dealing with multiple 'evals':
Code:
mpgw=13
CATEGORY=mpgw
Fred=CATEGORY

nawk -f jeff.awk myFile

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


Last edited by vgersh99; 07-02-2008 at 03:39 PM..
# 3  
Old 07-02-2008
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............
# 4  
Old 07-02-2008
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...
Code:
BEGIN {
   FS=OFS="="
}
# the rest of the script


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

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

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

# 5  
Old 07-02-2008
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.
# 6  
Old 07-02-2008
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
# 7  
Old 07-02-2008
It looks like I also have 'gawk' capability, and can use this instead.
Not sure what the difference is. Seems basically the same.

Last edited by jeffpas; 07-02-2008 at 05:57 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grepping for one variable while using awk to parse an associated variable

Im trying to search for a single variable in the first field and from that output use awk to extract out the lines that contain a value less than a value stored in another variable. Both the variables are associated with each other. Any guidance is appreciated. File that contains the... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

2. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

3. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

4. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: varT="1--2--3--5" i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT". but here's my awk code: awk -v valA="$varT" "BEGIN {print valA}" this prints the entire line. i feel like i'm so close to getting what i want. i... (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

6. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

7. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

8. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

9. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

10. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies
Login or Register to Ask a Question