How to coerce AWK to evaluate string as math expression?

血红的双手。 提交于 2019-11-29 05:19:10

awk lacks an eval(...) function. This means that you cannot do string to code translation based on input after the awk program initializes. Ok, perhaps it could be done, but not without writing your own parsing and evaluation engine in awk.

I would recommend using bc for this effort, like

[edwbuck@phoenix ~]$ echo "s(0.3)" | bc -l
.29552020666133957510

Note that this would require sin to be shortened to s as that's the bc sine operation.

You can just create your own eval function which calls awk again to execute whatever command you want it to:

$ cat tst.awk
{ print eval($1), sin($2) }

function eval(str,      cmd,line,ret) {
    cmd = "awk \047BEGIN{print " str "; exit}\047"
    if ( (cmd | getline line) > 0 ) {
        ret = line
    }
    close(cmd)
    return ret
}

$ echo 'sin(0.3) 0.3' | awk -f tst.awk
0.29552 0.29552

$ echo '4*7 0.3' | awk -f tst.awk
28 0.29552

$ echo 'tolower("FOO") 0.3' | awk -f tst.awk
foo 0.29552

With gawk version 4.1.2 :

echo "sin(0.3) 0.3" | awk '{split($1,a,/[()]/);f=a[1];print @f(a[2]),sin($2)}'

It's ok with tolower(FOO) too.

You can try Perl as it has eval() function.

$  echo "sin(0.3)" | perl -ne ' print eval '
0.29552020666134
$

For the given input,

$ echo "sin(0.3) 0.3" | perl -ne ' /(\S+)\s+(\S+)/ and print eval($1), " ", $2 '
0.29552020666134 0.3
$
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!