How to coerce AWK to evaluate string as math expression?

前端 未结 5 809
一生所求
一生所求 2020-12-17 06:25

Is there a way to evaluate a string as a math expression in awk?

balter@spectre3:~$ echo \"sin(0.3) 0.3\" | awk \'{print $1,sin($2)}\'
sin(0.3) 0.29552


        
相关标签:
5条回答
  • 2020-12-17 06:42

    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.

    0 讨论(0)
  • 2020-12-17 06:48

    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
    
    0 讨论(0)
  • 2020-12-17 06:53

    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.

    0 讨论(0)
  • 2020-12-17 06:54

    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
    $
    
    0 讨论(0)
  • 2020-12-17 07:04

    Here's a simple one liner!

    math(){ awk "BEGIN{printf $1}"; }
    

    Examples of use:

    math 1+1  
    

    Yields "2"

    math 'sqrt(25)'
    

    Yeilds "5"

    x=100; y=5; math "sqrt($x) + $y"
    

    Yeilds "15"

    0 讨论(0)
提交回复
热议问题