如何计算使用bash特定列的总和? [英] How Can I calculate the sum of a specific column using bash?

查看:142
本文介绍了如何计算使用bash特定列的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算使用bash特定列的总和,而无需使用特定的列打印(我想保持我的管道输出的所有列,只有总结其中之一!)

I want to calculate the sum of a specific column using bash without using the print that specific column (I want to keep all the output columns of my pipeline and only sum one of them!)

推荐答案

如果你想求和,比方说,第二列,但打印在一些管道中的所有列:

If you wanted to sum over, say, the second column, but print all columns in some pipeline:

cat data | awk '{sum+=$2 ; print $0} END{print "sum=",sum}'

如果该文件中的数据是这样的:

If the file data looks like:

1 2 3
4 5 6
7 8 9

那么输出将是:

1 2 3
4 5 6
7 8 9
sum= 15

这篇关于如何计算使用bash特定列的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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