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

查看:14
本文介绍了如何使用 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天全站免登陆