bash 在bash中添加两个变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28821710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:27:36  来源:igfitidea点击:

add two variables in bash

bashshellawk

提问by starpark

I have a fileA that contains:

我有一个包含以下内容的文件A:

  0012,001650,0089

I want to get the sum of column 1 and column 2 and the quotient of column 3 and 60. Using awk I tried this

我想得到第 1 列和第 2 列的总和以及第 3 列和 60 列的商。使用 awk 我试过这个

  col1=`awk -F, '{print }' $fileA | sed 's/0*//'`
  col2=`awk -F, '{print }' $fileA | sed 's/0*//'`
  col3=`awk -F, '{print }' $fileA | sed 's/0*//'`

  sum=$((col1 + col2))
  qou=$((col3 / 60))

But I got this error:

但我收到了这个错误:

apn_processing.sh[226]: col1 + col2: The specified number is not valid for this command.

apn_processing.sh[226]: col1 + col2: 指定的数字对该命令无效。

Can you give me another solution for this?

你能给我另一个解决方案吗?

回答by Ed Morton

shell is for sequencing calls to tools, that's all. If you're just doing text processing then do the whole job in awk:

shell 用于对工具调用进行排序,仅此而已。如果您只是在进行文本处理,那么在 awk 中完成整个工作:

$ awk -F, '{
  sum =  + 
  quo =  / 60
  print sum, quo
}' "$fileA"
1662 1.48333

回答by glenn Hymanman

You don't need to call awk 3 times to extract the fields:

您不需要调用 awk 3 次来提取字段:

$ IFS=, read -r a b c < file
$ echo $a
0012
$ echo $b
001650
$ echo $c
0089
$ echo $((10#$a + 10#$b))
1662
$ echo $((10#$c / 60))
1
$ bc  <<< "scale=3; $c/60"
1.483

In bash math, you can specific a number's base with the notation base#number, so you don't get tripped up by invalid octal numbers like "0089"

在 bash 数学中,您可以使用符号指定数字的基数base#number,这样您就不会被无效的八进制数字(如“0089”)绊倒

回答by Tom Fenech

Especially since you're dealing with floating point arithmetic, I would recommend performing the calculations in awk:

特别是因为您正在处理浮点运算,我建议您在 awk 中执行计算:

sum=$(awk -F, '{print +}' file)
qou=$(awk -F, '{print /60}' file)

回答by John1024

Assuming that you have reason to want to get the answers into shell variables:

假设您有理由想要将答案放入 shell 变量中:

$ read sum quo < <(awk -F, '{print (+), /60}' "$fileA")
$ echo $sum $quo
1662 1.48333

Unlike bash, awkhas no problem with leading zeros on numbers that it receives as input. For example, observe that the following works just fine:

与 不同的是bashawk它在作为输入接收的数字上使用前导零没有问题。例如,观察以下工作正常:

$ echo 016 08 | awk '{print , , +, /}'
016 08 24 2

Aside

在旁边

mklement0 points out in the comments that GNU awkwill try to interpret numbers beginning with zero as octal, falling back to decimal, if they are program constants as opposed to input. Consider:

mklement0 在评论中指出awk,如果它们是程序常量而不是输入,则GNU会尝试将以零开头的数字解释为八进制,然后返回到十进制。考虑:

$ awk 'BEGIN{a=016;b=08; print a, b, a+b, a/b}'
14 8 22 1.75

In the above, 016is treated as octal. By contrast, 08, which is not a legal octal number, is treated as decimal.

在上面,016被视为八进制。相比之下,08不是合法的八进制数的 被视为十进制数。