How to coerce AWK to evaluate string as math expression?

前端 未结 5 811
一生所求
一生所求 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: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
    $
    

提交回复
热议问题