I would like to run a script within an awk command. I have a code ./myscript that calculates the likelihood between two entities; the results are listed in listfile.txt.
Use awk's system() function:
Here an example
awk '{printf("%s ",$1); system("myscript " $2)}' file
the example is from this site https://unix.stackexchange.com/questions/72935/using-bash-shell-function-inside-awk
I think I see your dilemma. Try this:
function getvalue()
{
local -a rms
./rand_input_generator
./myscript
# No need for invoking gawk -- use bash array
read -a rms < listfile.txt
# Output eighth column/word
echo ${rms[7]}
# Echo to stderr/terminal
echo ${rms[7]} 1>&2
}
rrr=$(getvalue)
min=0.01;
max=0.5;
# Let awk do the comparison, and print out "true" command or "false" command,
# evaluate the command, and loop based on return code
while $(awk -v rms="${rrr}" -v min="${min}" -v max="${max}" 'BEGIN {if (rms < min || rms > max) print "true"; else print "false"}'); do
# Refresh the value
rrr=$(getvalue)
done
In reality awk
is really a string-processing language, not a math language, so I'd recommend this change to the last three lines if you have bc
:
# Call bc to evaluate expression, returning 1 or 0 based on result, and check
while [[ $(echo "(${rrr} < ${min}) || (${rrr} > ${max})" | bc) -eq 1 ]]; do
rrr=$(getvalue)
done
Use awk's system
function. The return value is the exit code.
!($8 >= 0 && $8 <= 0.05) { system("./myscript") }