Operating System - HP-UX
1843992 Members
1850 Online
110226 Solutions
New Discussion

Shell Variable in Awk Script

 
Vassilios
Frequent Advisor

Shell Variable in Awk Script

Dear All:

I'm trying to put a shell variable into an awk script, but its not working:

unix-prompt> NAME="root"
unix-prompt> export NAME
unix-prompt> echo $NAME
unix-promot> root

unix-prompt> w | awk '$1 == $NAME {print $1 $2}'

This doesnt work!
I've tried to put $NAME in double quotes, single quotes, single and double quotes, quotes with backslashes, forward slashes, within brackets, without brackets, oufff... i've tried everything.

This works:

unix-prompt> w | awk '$1 == "root" {print $1 $2}

--> This will only output all users who are root. But why doesnt the above syntax accept a shell variable.

Thanks
12 REPLIES 12
Dietmar Konermann
Honored Contributor

Re: Shell Variable in Awk Script

To access the envirnonment variable from an awk script you may use ENVIRON["..."].

In your example:
w | awk '$1 == ENVIRON["NAME"] {print $1 $2}'

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Vibhor Kumar Agarwal
Esteemed Contributor

Re: Shell Variable in Awk Script

There is one option -v.

You can try this also.
Vibhor Kumar Agarwal
Alex Lavrov.
Honored Contributor

Re: Shell Variable in Awk Script

NAME="alex"

awk '{print a}' a=$NAME
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Vassilios
Frequent Advisor

Re: Shell Variable in Awk Script

OK. well, i tried the -v option, and that does nothing.

The gentleman from HP that suggested to use the ENVIRON[] command within the string; will this work within a shell variable?? I tried the syntax that you mentioned, and that doesnt work.

I dont particulary want to "who" the users, and find which ones are root; its just an example that has to work in a shell script where variable $NAME is set in a /bin/sh script file.

i've tried this:

w | awk -v uzer=$NAME '$1 == $uzer {print $1}'

This does nothing for me either. The global or shell variable has to be given to the awk first field ($1) as it must satisfy the conditiion.

Thanks for any more suggestions.
Dietmar Konermann
Honored Contributor

Re: Shell Variable in Awk Script

Vassilios,

You need to export the shell variable, of course:

$ export NAME=dkonerma
$ w | awk '$1 == ENVIRON["NAME"] {print $1, $2}'
dkonerma pts/0
dkonerma pts/1
dkonerma pts/2

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Dietmar Konermann
Honored Contributor

Re: Shell Variable in Awk Script

Ah... and if you use the -v option, leave away the "$" inside your awk script:

w | awk -v uzer=$NAME '$1 == uzer {print $1}'

"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Alex Lavrov.
Honored Contributor

Re: Shell Variable in Awk Script

Maybe my reply was not clear enough. You can send a parameter to awk like this:

NAME="alex"

awk '{print a}' a=$NAME

No, "a" has the value of $NAME and you can use it in awk.
I don't give a damn for a man that can only spell a word one way. (M. Twain)
Vassilios
Frequent Advisor

Re: Shell Variable in Awk Script

OK People. I found the answer the myself!!
I'm not saying what the answer would be cos that would be telling. HAHA.

Thanks a lot for those who tried to answer. Can someone please award me 1000's of points.

Cheers
Dietmar Konermann
Honored Contributor

Re: Shell Variable in Awk Script

Would be nice to hear the correct question... since all answers I read in this thread are quite right.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Vassilios
Frequent Advisor

Re: Shell Variable in Awk Script

OK, I just gave u 5 points as u came up with some good suggestions. But it took me ages to figure out the correct number of double quotes and little punctuation marks that make that script in awk work... and if i mentioned how it was done, how many points would i get?? See?? No one would award me any points for answering my own questions?? That's what its like here. The little guy suffers :( We give, but we never receive.

Vibhor Kumar Agarwal
Esteemed Contributor

Re: Shell Variable in Awk Script

Vassilios,

We are not here for the point system.
But just to exchange knowledge.

It will be better if you clear your concept.
Vibhor Kumar Agarwal
Alex Lavrov.
Honored Contributor

Re: Shell Variable in Awk Script

Vassilios,

With all due respect, this is not a trivia game. You can assign points to the ppl that took some of their time to answer your question and assign even more the the ppl that their answers you found the most helpful. We all do sometimes answer our own questions, in that case, just put here the answer and close the thread, so it will remain in the database and help ppl when their search for the similar problem.

Btw, all the answers supplid here are working, so it's better that you'll assign some points and close this thread.


Alex.
I don't give a damn for a man that can only spell a word one way. (M. Twain)