- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Capturing awk "system" command to awk variable?
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
Discussions
Discussions
Discussions
Forums
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
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-26-2004 01:07 AM
тАО08-26-2004 01:07 AM
How can I capture the output into an awk variable? I know this can be done in perl etc, but I'm using awk, so...
eg (in a simple case): -
awk '{fred=`system("df $1")` ; split(fred,bill) ; print $1" uses "bill[4]" blocks."}' filesystems
I know THIS example can be done in other ways, but it's taken as part of a larger context in awk which is why I need awk.
Any ideas?
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2004 01:39 AM
тАО08-26-2004 01:39 AM
Solution"whoami" | getline
Will execute "whoami" and put the result into $0 (and also parse for fields).
"whoami" | getline myvar
Will instead put the result into myvar.
HTH
-- Rod Hills
- Tags:
- getline
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2004 01:55 AM
тАО08-26-2004 01:55 AM
Re: Capturing awk "system" command to awk variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2004 02:00 AM
тАО08-26-2004 02:00 AM
Re: Capturing awk "system" command to awk variable?
"Finally, cmd | getline pipes the output of cmd into getline; each call of getline returns the next line of output from cmd."
Example:
awk 'BEGIN{while ("ps" | getline){ print "test " $1 " test"}}' /dev/null | more
This is similar to the following hack:
mknod x
awk 'BEGIN{system("ps >> x&"); while (getline < "x"){ print "test " $1 " test"}}' /dev/null
rm x
The are (gawk) extensions (Tru64) that have more support in this area. Like a native "&",
and close(file[, how]) to close files, pipes, or (two-way) pipse to co-processes.
Enjoy,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-26-2004 02:26 AM
тАО08-26-2004 02:26 AM
Re: Capturing awk "system" command to awk variable?
answers to the question how to get
systemoutput into a variable(!).
So here is the answer:
DO NOT USE system(), but escape back
to the shell:
awk '{ ...
x="'"`date`"'"
printf "%s\n",x
}'
Regards,
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-26-2006 01:39 PM
тАО12-26-2006 01:39 PM
Re: Capturing awk "system" command to awk variable?
How a dynamic string can be pushed in the command e.g. du
Thanks,
Kuntal
- Tags:
- variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-26-2006 02:21 PM
тАО12-26-2006 02:21 PM
Re: Capturing awk "system" command to awk variable?
Welcome to the Forums! You should really not resurrect old threads, but rather post your own queries. That said, however, you asked:
> How a dynamic string can be pushed in the command e.g. du
# awk -v DIR="/tmp" 'END{while ("du -s " DIR|getline) {print}}' /dev/null
Note that there is a space (blank) character after the '-s' flag and before the variable passed from the shell to 'awk' --- DIR.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-27-2006 03:00 AM
тАО12-27-2006 03:00 AM
Re: Capturing awk "system" command to awk variable?
Thanks for the respose. I think, my question was not clear enough.
I have scenario such that instead of putting some static command 'date', I need to pass some dynamic string as a command - du
awk '{ ...
x="'"`date`"'"
printf "%s\n",x
}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО01-03-2007 05:49 PM
тАО01-03-2007 05:49 PM
Re: Capturing awk "system" command to awk variable?
Can you evaluate the command before you invoke awk? If not, you could invoke awk, execute the command and then go back in awk.