我正在寻找一个命令,它将接受(作为输入)多行文本,每行包含一个整数,并输出这些整数的和。

作为一点背景知识,我有一个日志文件,其中包括计时测量。通过grepping的相关行和一点sed重新格式化,我可以列出该文件中的所有时间。我想算出总数。我可以将这个中间输出输出到任何命令,以便进行最终求和。我过去一直使用expr,但除非它在RPN模式下运行,否则我不认为它会处理这个问题(即使这样也会很棘手)。

我怎样才能得到整数的和?


当前回答

所有基于fedora的系统[fedora,RHEL,CentOS,korora等]上都有lua解释器,因为它嵌入了rpm-package(包管理器rpm的包),即rpm-lua],如果你想学习lua,这种问题是理想的(你也会完成你的工作)。

cat filname | lua -e "sum = 0;for i in io.lines() do sum=sum+i end print(sum)"

这很有效。虽然Lua很啰嗦,但你可能不得不忍受一些反复敲击键盘的伤害:)

其他回答

实时求和,让您监视某些数字处理任务的进度。

$ cat numbers.txt 
1
2
3
4
5
6
7
8
9
10

$ cat numbers.txt | while read new; do total=$(($total + $new)); echo $total; done
1
3
6
10
15
21
28
36
45
55

(在这种情况下,不需要将$total设置为0。你也不能在完成后访问$total。)

perl -lne '$x += $_; END { print $x; }' < infile.txt

更新的基准

所以我合成了随机分布的100mn个整数

之间的

0^0 - 1 

and

8^8 - 1

代码生成器

mawk2 '
BEGIN {
     __=_=((_+=_^=_<_)+(__=_*_*_))^(___=__)
     srand()
     ___^=___
     do  { 
           print int(rand()*___) 
  
     } while(--_)  }' | pvE9 > test_large_int_100mil_001.txt

     out9:  795MiB 0:00:11 [69.0MiB/s] [69.0MiB/s] [ <=> ]

  f='test_large_int_100mil_001.txt'
  wc5 < "${f}"

    rows = 100000000. | UTF8 chars = 833771780. | bytes = 833771780.

最后一位的奇/偶分布

Odd  49,992,332
Even 50,007,668

AWK -最快的,有很大的优势(可能C更快,我不知道)

in0:  795MiB 0:00:07 [ 103MiB/s] [ 103MiB/s] [============>] 100%            
( pvE 0.1 in0 < "${f}" | mawk2 '{ _+=$__ } END { print _ }'; )  

 7.64s user 0.35s system 103% cpu 7.727 total
     1  838885279378716

Perl -相当不错

 in0:  795MiB 0:00:10 [77.6MiB/s] [77.6MiB/s] [==============>] 100%            
( pvE 0.1 in0 < "${f}" | perl -lne '$x += $_; END { print $x; }'; )  
 
10.16s user 0.37s system 102% cpu 10.268 total

     1  838885279378716

Python3——稍微落后于Perl

 in0:  795MiB 0:00:11 [71.5MiB/s] [71.5MiB/s] [===========>] 100%            
( pvE 0.1 in0 < "${f}" | python3 -c ; )  

 11.00s user 0.43s system 102% cpu 11.140 total
     1  838885279378716

RUBY -不错

 in0:  795MiB 0:00:13 [61.0MiB/s] [61.0MiB/s] [===========>] 100%            
( pvE 0.1 in0 < "${f}" | ruby -e 'puts ARGF.map(&:to_i).inject(&:+)'; )  
15.30s user 0.70s system 101% cpu 15.757 total

     1  838885279378716

JQ -慢

 in0:  795MiB 0:00:25 [31.1MiB/s] [31.1MiB/s] [========>] 100%            
( pvE 0.1 in0 < "${f}" | jq -s 'add'; )  

 36.95s user 1.09s system 100% cpu 37.840 total

     1  838885279378716

DC

- ( had to kill it after no response in minutes)

提前为反勾号(“'”)的可读性道歉,但这些在shell中工作,而不是bash,因此更易于粘贴。如果你使用一个接受它的shell, $(command…)格式比' command…所以为了你的理智,请随意修改。

我在bashrc中有一个简单的函数,它将使用awk来计算一些简单的数学项

calc(){
  awk 'BEGIN{print '"$@"' }'
}

这将做 +,-,*,/,^,%, √6,罪恶,因为,括号……(取决于你的awk版本)…你甚至可以用printf和格式化浮点输出,但这是我通常需要的

对于这个特定的问题,我将对每一行简单地这样做:

calc `echo "$@"|tr " " "+"`

所以对每一行求和的代码块看起来像这样:

while read LINE || [ "$LINE" ]; do
  calc `echo "$LINE"|tr " " "+"` #you may want to filter out some lines with a case statement here
done

如果你想逐行求和的话。但是,对于数据文件中的每个数字的总数

VARS=`<datafile`
calc `echo ${VARS// /+}`

顺便说一句,如果我需要在桌面上快速做一些事情,我使用这个:

xcalc() { 
  A=`calc "$@"`
  A=`Xdialog --stdout --inputbox "Simple calculator" 0 0 $A`
  [ $A ] && xcalc $A
}

Python中的一行代码版本:

$ python -c "import sys; print(sum(int(l) for l in sys.stdin))"