- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Shell Variable in Awk Script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 10:47 PM
08-03-2005 10:47 PM
Shell Variable in Awk Script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 10:51 PM
08-03-2005 10:51 PM
Re: Shell Variable in Awk Script
In your example:
w | awk '$1 == ENVIRON["NAME"] {print $1 $2}'
Best regards...
Dietmar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 10:52 PM
08-03-2005 10:52 PM
Re: Shell Variable in Awk Script
You can try this also.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 10:53 PM
08-03-2005 10:53 PM
Re: Shell Variable in Awk Script
awk '{print a}' a=$NAME
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 11:00 PM
08-03-2005 11:00 PM
Re: Shell Variable in Awk Script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 11:11 PM
08-03-2005 11:11 PM
Re: Shell Variable in Awk Script
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 11:13 PM
08-03-2005 11:13 PM
Re: Shell Variable in Awk Script
w | awk -v uzer=$NAME '$1 == uzer {print $1}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 11:14 PM
08-03-2005 11:14 PM
Re: Shell Variable in Awk Script
NAME="alex"
awk '{print a}' a=$NAME
No, "a" has the value of $NAME and you can use it in awk.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 11:27 PM
08-03-2005 11:27 PM
Re: Shell Variable in Awk Script
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2005 11:36 PM
08-03-2005 11:36 PM
Re: Shell Variable in Awk Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2005 12:47 AM
08-04-2005 12:47 AM
Re: Shell Variable in Awk Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2005 12:50 AM
08-04-2005 12:50 AM
Re: Shell Variable in Awk Script
We are not here for the point system.
But just to exchange knowledge.
It will be better if you clear your concept.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2005 12:55 AM
08-04-2005 12:55 AM
Re: Shell Variable in Awk Script
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.