I have the same problem and I the solutions posted here don't work for me or I don't understand them So, how can I retrive in awk the returned value by a shell command and not its return status (0,1)?
My problem: I want to perform some date manipulation inside awk. For this, I want to convert the dates in the number of seconds using the date shelll command.
Thanks for the reply but I get an error when using getline. Is getline an standard UNIX command (I'm using cygwin :-()? Can I use it inside an awk code?
This actually expects TWO parameters passed to it - you're just passing ONE (d1). Where's the second?
Do you get anything spilled to stderr complaining about 'sprintf'?
What's this '+%%s' supposed to be?
%% is used for escaping the special character %, so %%s wouldn't expect any command ant it will print the % character.
sprintf("date -d %s +%%s",d1)
will print
date -d <value for d1> +%s
which is the command I need for converting the date in format
mm/dd/yyyy in a number (number of seconds) to be able for performing
comparations between dates (which date goes after the oter....)
Here is an example. All code goes into your awk script ...
The value of the current line's $1 will be inserted into the cmd variable.
The string in the cmd variable will be executed in the unix shell by awk's getline, and the command's output will be saved in var (a newly created awk variable).
Awk will have access to the command's output via the var variable.
cmd="echo "$1" | tr [:upper:] [:lower:]"
cmd | getline var
close(cmd)
print var