Bash 命令对一列数字求和 [英] Bash command to sum a column of numbers

查看:42
本文介绍了Bash 命令对一列数字求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

cat FileWithColumnOfNumbers.txt | sum

推荐答案

使用现有文件:

paste -sd+ infile | bc

使用标准输入:

<cmd> | paste -sd+ | bc

对于一些粘贴实现,从stdin读取时需要更加明确:

With some paste implementations you need to be more explicit when reading from stdin:

<代码>|粘贴 -sd+ - |bc

使用的选项:

-s (serial) - 将所有行合并为一行

-s (serial) - merges all the lines into a single line

-d - 使用非默认分隔符(在本例中为字符 +)

-d - use a non-default delimiter (the character + in this case)

这篇关于Bash 命令对一列数字求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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