LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-12-2007, 04:12 AM   #1
zoshr
LQ Newbie
 
Registered: Feb 2007
Posts: 12

Rep: Reputation: 0
an awk question


I have the following problem. I want to use awk to parse a debug trace: it is a text file,
with each line having several contents of variables separated by spaces, something like this:

x y z ...
x y z ...

I want to do the following: to specify at command prompt to my awk script which variable
to be printed out by "-v var=VARIABLE". In the script I use an associative array to store
variables, something like this:

debug_array[x]=$1
debug_array[y]=$2
debug_array[z]=$3

If I now use the passed variable name in the var to extract content from the debug_array, I
do the following:

debug_array[var]


The problem with this is that debug_array is literally indexed by "var", not by its content
(e.g. x,y or z). If I now alter the previous line to:

debug_array[$var]

all I get is a blank output. If I use literally inside the script the intended variable name
e.g.:

debug_array[x]

everything is fine. What's wrong here?

TIA

Last edited by zoshr; 04-12-2007 at 04:20 AM.
 
Old 04-12-2007, 07:33 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
That should be "-v var=$VARIABLE".
 
Old 04-12-2007, 08:02 AM   #3
zoshr
LQ Newbie
 
Registered: Feb 2007
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jlliagre
That should be "-v var=$VARIABLE".
Thanks for the reply,

I suppose my description by "-v var=$VARIABLE" was not the best one.
What I meant was: at the command prompt this would be "-v var=x" or
"-v var=y" or "-v var=z" etc, depending on which column I want to be
printed out. I know one could solve this by applying the cut command,
on the trace file e.g.

cut -f3 -d" " FILE

but in this way I would have to know which column in the trace
file corresponds to which variable(x,y or z) that is traced. I
want to have a solution where I directly specify the name of
variable I'm interested in, without constantly being bothered with
the column number where this variable trace is located. Instead
this mapping is written once for all in the script itself, all
I would have to do is to specify the name of variable, I would
like printed out (humans are better with names than with numbers,
we'll leave numbers to computers). So the main problem I face
here is how to tell awk that the string in the variable var should
be used as an index into debug_array to fetch value of the variable
x|y|z from the file, not the literal string "var". I have tried this:

print debug_array[$var]

but it doesn't work as I would like it to work.

Last edited by zoshr; 04-12-2007 at 08:04 AM.
 
Old 04-12-2007, 03:47 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Sorry, but I still don't understand what you are trying to achieve.

Please post full real code exhibiting the issue.
 
Old 04-12-2007, 04:00 PM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Are the variables in fixed positions or do you need to match the variable name on the output itself and then use the next item?

If the former is true, you could assign constants to variable names like this:
xcol=2
ycol=3

and then use the value of the argument to assign the field that you are interested in, depending on the value of the argument.

Posting a couple of lines of the debug trace might be useful then the general description you provided. Of course you could edit things like IP address or usernames with fake values if there are any security or privacy concerns.

If you are writing a script to call the awk script, please indicate which language you use. I'm guessing it is perl and not bash. Is that important or would a bash script be ok or do you want an awk only script.
 
Old 04-13-2007, 06:29 AM   #6
zoshr
LQ Newbie
 
Registered: Feb 2007
Posts: 12

Original Poster
Rep: Reputation: 0
Thumbs up

Thanks guys for the replys,

The trace looks like this:

...
1 1 304
7 5 209
...

Therefore: x=1,7; y=1,5; z=304,209.

I have found an error I made. When assigning the field values to the debug_array such as:

debug_array[x]=$1;
debug_array[y]=$2;
debug_array[z]=$3;

where x, y, z are variables whose values are found in the trace, I should have used the following:

debug_array["x"]=$1;
debug_array["y"]=$2;
debug_array["z"]=$3;

Note the quotes surrounding the trace's variable name. Now I can easily fetch the column of interest by specifying -v var=[x|y|z] at the command prompt, as at the end of script the following is performed:

print debug_array[var]

This prints out the y related column from the trace file.

Thanks for your help & assistance!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk question davidkline Linux - General 9 09-29-2006 01:37 PM
question about awk perfect_circle Programming 4 07-19-2006 03:34 PM
Awk question blanny Linux - Software 6 05-02-2006 09:03 AM
awk question jonlake Linux - Software 2 05-04-2005 04:58 PM
awk question dilberim82 Programming 2 10-01-2003 08:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration