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
# 8  
Old 07-02-2008
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......Smilie
# 9  
Old 07-02-2008
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.
# 10  
Old 07-02-2008
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.

Last edited by jeffpas; 07-02-2008 at 07:37 PM..
# 11  
Old 07-02-2008
Tools

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
# 12  
Old 07-02-2008
Quote:
Originally Posted by jeffpas
and basically all I have to do is figure out how to get the value of a variable inside a variable.
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:

Code:
x="abc"
print - "$x"

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

Code:
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":

Code:
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:

Code:
mpgw=13
CATEGORY=mpgw

eval print - "\$CATEGORY"

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

Code:
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

Last edited by bakunin; 07-02-2008 at 11:45 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