开发者

Linux shell sum columns

How can I sum (using Linux shell) numbers in a column? If possible, I don't want to use powerful tools like awk or perl. I want som开发者_StackOverflow社区ething like giveMeNumber | sum


Since you didn't provide file sample, I'll show the simple (1st column) approach, using Unix's calculator bc and transforming newlines into "+" using tr command - you need an extra 0 at the end since the newline after the last number will create an extra "+" at the very end:

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

However, I'm not quite sure why you need to avoid Perl, as it's very easy to do as a one-liner:

> perl5.8 -ae '{map {$sum+=$_} <>; print "$sum\n"}' a
6


giveMeNumber | (tr '\n' '+'; echo 0) | bc

Assuming that giveMeNumber generates one number per line in the first column, use cut to extract the appropriate column otherwise. The tr command will replace ever newline character with a plus sign:

1
2
3

becomes

1+2+3+

the echo 0 at the end is needed to prevent a parse error from the final + at the end. All of this is fed to bc which sees an arithmetic expression which effectively sums the numbers and prints the result:

$ seq 10|(tr '\n' '+'; echo 0)|bc
55


Why do you want to avoid awk? It's pretty simple to do, and you can easily change which column to sum by changing $1 to $2, $3, etc.

$ seq 10
1
2
3
4
5
6
7
8
9
10
$ seq 10 | awk '{sum += $1} END{print sum}'
55


Well - if you're using bash (which is probably the most common shell used on Linux, but there isn't actually a single "Linux shell" :-) you could do something like the following:

#!/bin/bash
a=0
while read n; do
  a=$[a+n]
done
echo $a

This would be your sum program - though it's probably a good idea to call it something else since sum is a standard utility for calculating checksums.


You can do it with Alacon - command-line utility for Alasql database. It works with Node.js, so you need to install Node.js and then Alasql package:

To calculate sum from stdin you can use the following command:

> cat data.txt | node alacon "SELECT VALUE SUM([0]) FROM TXT()" 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜