Bash 命令对一列数字求和

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3096259/
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-09 19:23:02  来源:igfitidea点击:

Bash command to sum a column of numbers

bash

提问by Jubal

I want a bash command that I can pipe into that will sum a column of numbers. I just want a quick one liner that will do something essentially like this:

我想要一个 bash 命令,我可以通过管道输入它来对一列数字求和。我只想要一个快速的班轮,基本上可以做这样的事情:

cat FileWithColumnOfNumbers.txt | sum

回答by Dimitre Radoulov

Using existing file:

使用现有文件:

paste -sd+ infile | bc

Using stdin:

使用标准输入:

<cmd> | paste -sd+ | bc

Edit: With some pasteimplementations you need to be more explicit when reading from stdin:

编辑:使用一些粘贴实现,从stdin读取时需要更加明确:

<cmd> | paste -sd+ - | bc

<cmd> | paste -sd+ - | bc

回答by ghostdog74

I like the chosen answer. However, it tends to be slower than awk since 2 tools are needed to do the job.

我喜欢选择的答案。然而,它往往比 awk 慢,因为需要 2 个工具来完成这项工作。

$ wc -l file
49999998 file

$ time paste -sd+ file | bc
1448700364

real    1m36.960s
user    1m24.515s
sys     0m1.772s

$ time awk '{s+=}END{print s}' file
1448700364

real    0m45.476s
user    0m40.756s
sys     0m0.287s

回答by Jonathan Leffler

Does two lines count?

两行算不算?

awk '{ sum += ; }
     END { print sum; }' "$@"

You can then use it without the superfluous 'cat':

然后,您可以在没有多余的“猫”的情况下使用它:

sum < FileWithColumnOfNumbers.txt
sum   FileWithColumnOfNumbers.txt

FWIW: on MacOS X, you can do it with a one-liner:

FWIW:在 MacOS X 上,您可以使用 one-liner:

awk '{ sum += ; } END { print sum; }' "$@"

回答by minhas23

The following command will add all the lines(first field of the awk output)

以下命令将添加所有行(awk 输出的第一个字段)

awk '{s+=} END {print s}' filename

回答by Dimitre Radoulov

[a followup to ghostdog74s comments]

[ghostdog74s评论的后续]

bash-2.03$ uname -sr
SunOS 5.8

bash-2.03$ perl -le 'print for 1..49999998' > infile

bash-2.03$ wc -l infile
 49999998 infile

bash-2.03$  time paste -sd+ infile | bc
bundling space exceeded on line 1, teletype
Broken Pipe

real    0m0.062s
user    0m0.010s
sys     0m0.010s

bash-2.03$ time nawk '{s+=}END{print s}' infile
1249999925000001

real    2m0.042s
user    1m59.220s
sys     0m0.590s
bash-2.03$ time /usr/xpg4/bin/awk '{s+=}END{print s}' infile
1249999925000001

real    2m27.260s
user    2m26.230s
sys     0m0.660s

bash-2.03$ time perl -nle'
  $s += $_; END { print $s }
   ' infile
1.249999925e+15

real    1m34.663s
user    1m33.710s
sys     0m0.650s

回答by DVK

You can use bc (calculator). Assuming your file with #s is called "n":

您可以使用 bc(计算器)。假设带有#s 的文件称为“n”:

$ cat n
1
2
3
$ (cat n | tr "2" "+" ; echo "0") | bc 
6

The trchanges all newlines to "+"; then we append 0 after the last plus, then we pipe the expression (1+2+3+0) to the calculator

tr所有换行符更改为“+”;然后我们在最后一个加号后附加 0,然后我们将表达式 ( 1+2+3+0) 通过管道传输到计算器

Or, if you are OK with using awk or perl, here's a Perl one-liner:

或者,如果您可以使用 awk 或 perl,这里有一个 Perl 单行:

$perl -nle '$sum += $_ } END { print $sum' n
6

回答by Paused until further notice.

while read -r num; do ((sum += num)); done < inputfile; echo $sum

回答by t6d

Use a forloop to iterate over your file …

使用for循环遍历您的文件……

sum=0; for x in `cat <your-file>`; do let sum+=x; done; echo $sum

回答by Joe Cannatti

If you have ruby installed

如果你安装了 ruby

cat FileWithColumnOfNumbers.txt | xargs ruby -e "puts ARGV.map(&:to_i).inject(&:+)"

回答by paco

[root@pentest3r ~]# (find / -xdev -size +1024M) | (while read a ; do aa=$(du -sh $a | cut -d "." -f1 ); o=$(( $o+$aa )); done; echo "$o";)