Operating System - Linux
1752520 Members
4893 Online
108788 Solutions
New Discussion юеВ

Capturing awk "system" command to awk variable?

 
SOLVED
Go to solution
Simon Hargrave
Honored Contributor

Capturing awk "system" command to awk variable?

Within awk, there is the system() function that lets me run a system command. However this only lets me capture the return code.

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?
8 REPLIES 8
Rodney Hills
Honored Contributor
Solution

Re: Capturing awk "system" command to awk variable?

You can use the "getline" function-

"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
There be dragons...
Simon Hargrave
Honored Contributor

Re: Capturing awk "system" command to awk variable?

Spot on, cheers!
Hein van den Heuvel
Honored Contributor

Re: Capturing awk "system" command to awk variable?

man awk....

"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.
Ceesjan van Hattum
Esteemed Contributor

Re: Capturing awk "system" command to awk variable?

The solutions above soes not give you
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
Kuntal Roy
New Member

Re: Capturing awk "system" command to awk variable?

Hi Ceesjan,

How a dynamic string can be pushed in the command e.g. du -s ...

Thanks,
Kuntal
James R. Ferguson
Acclaimed Contributor

Re: Capturing awk "system" command to awk variable?

Hi Kuntal:

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 -s ...

# 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...

Kuntal Roy
New Member

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 -s | awk '{print $1}'. I am already inside an awk command.

awk '{ ...
x="'"`date`"'"
printf "%s\n",x
}'

Dennis Handly
Acclaimed Contributor

Re: Capturing awk "system" command to awk variable?

>Kuntal: I have scenario such that instead of putting some static command 'date',

Can you evaluate the command before you invoke awk? If not, you could invoke awk, execute the command and then go back in awk.