sed 似乎并没有一致地替代 - * 失败

sed 似乎并没有一致地替代 - * 失败

我正在尝试编写一个循环,按照操作出现的顺序(而不是通常的数学优先顺序)计算操作,代码如下所示(echo 用于调试):

while [[ "$(echo "$newstring"| grep -E ^-?[0-9]+$)" = "" ]]; do
       oldpart="$(echo "$newstring"|cut -f1-3 -d' ')"
       echo "bla $oldpart"
       newpart="$(echo "$oldpart"|bc)"
       echo "ble $newpart"
       newstring="$(echo "$newstring"|sed -e "s/$oldpart/$newpart/")"
       echo "bli $newstring"
done

当 $newstring 作为“6 + 6 * 9”传递时,输出如下:

6 + 6 * 9
bla 6 + 6
ble 12
bli 12 * 9
bla 12 * 9
ble 108
bli 12 * 9
bla 12 * 9
ble 108

我们可以看到,6 + 6 按预期计算为 12,然后替换到字符串中。然后该操作重新启动 12 * 9, 108 - 无法替换为字符串......而 while 永远不会结束

我怀疑 sed 解释 * 这可能会阻止所需的替换。

知道如何绕过这种行为吗?

答案1

bash无需外部命令即可使用的解决方案

#!/bin/bash
newstring='6 + 6 * 9'

read -a atoms <<<"$newstring"
run=${atoms[0]}               # Initialise running total to the first value

for ((i=1; i<=${#atoms[@]}; i+=2))
do
    op=${atoms[$i]}           # Next operator
    num=${atoms[$((i+1))]}    # Next number
    run=$((run $op num))      # Perform the arithmetic (integer maths)
done
echo "$run"

如果您想使用浮点运算,则需要使用bcdc。此变体使用dc, 作为在评论中建议

#!/bin/bash
newstring='6.5 + 6 * 9'

{
    read -a atoms <<<"$newstring"

    run=${atoms[0]}
    printf "%s " "$run"

    for ((i=1; i<=${#atoms[@]}; i+=2))
    do
        op=${atoms[$i]} num=${atoms[$((i+1))]}
        printf " %s %s" "$num" "$op"
    done
    printf " p\n"
} | dc

相关内容