评估在awk中作为参数传递的算术表达式 [英] evaluate arithmetic expression passed as argument in awk

查看:59
本文介绍了评估在awk中作为参数传递的算术表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算在awk中作为参数传递的算术表达式?

How to evaluate arithmetic expression passed as argument in awk?

我有这个文件.

1*2*3
1+2*3

传入此文件后,awk应该输出6、7.

awk should output 6, 7, when this file is passed in.

推荐答案

awk(1)是错误的工具,因为它没有eval()函数.我建议将文件修改为bc(1)的输入或使用shell算术扩展:

awk(1) is the wrong tool as it doesn't have an eval() function. I suggest modifying the file into input for bc(1) or using shell arithmetic expansion:

while read expr; do
    echo "$(($expr))"
done < file

这篇关于评估在awk中作为参数传递的算术表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆