How to execute (./myscript) inside awk or bash script?

前端 未结 3 1222
逝去的感伤
逝去的感伤 2021-01-23 09:39

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.

相关标签:
3条回答
  • 2021-01-23 10:22

    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

    0 讨论(0)
  • 2021-01-23 10:37

    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
    
    0 讨论(0)
  • 2021-01-23 10:42

    Use awk's system function. The return value is the exit code.

    !($8 >= 0 && $8 <= 0.05) { system("./myscript") }
    
    0 讨论(0)
提交回复
热议问题