How to capture system() function output in awk variable and the print that awk variable.....
A system() function is a shell.
If the order doesn't matter,
it is usually better to first start with a shell, then invoke awk from there and pass arguments. Example:
#!/bin/sh
x=$(whatever command)
awk -v x="$x" 'awk code can use the x variable'
Thanks
Yeah I agree with you...but I want to know how to store result of system() function..........
All you can capture into a variable is the exit code of the command executed by the system function, like A=system("ls")
. The output of ls
is printed to stdout immediately.
Ha soo we cannot capture commands output instead of status.....okay thanks for quick reply
---------- Post updated at 11:34 AM ---------- Previous update was at 11:29 AM ----------
Soo there is no way to capture result? ????:(:(:(
You can store the output of an external command invoked from inside awk,
but you should not use the system function for that:
zsh-5.0.2[user]% awk 'BEGIN { "date" | getline dt; print "Got", dt }'
Got Thu, Jul 31, 2014 1:36:47 PM
:):) thanks
For multi-line output use a loop:
zsh-5.0.2[user]% awk 'BEGIN {
while (("ls /etc | head" | getline _out) > 0)
print "Got:", _out
}'
Got: alternatives
Got: bash.bash_logout
Got: bash.bashrc
Got: bash_completion
Got: bash_completion.d
Got: csh.cshrc
Got: csh.login
Got: dbus-1
Got: defaults
Got: DIR_COLORS