1

I do a cat results.txt | grep eval and I get

eval: -2.72907
baseline eval (random): -0.031584202184
eval: 0.807805
baseline eval (random): 0.0227601966463
eval: 2.0625
baseline eval (random): 0.0138953249621

How do I sum eval and baseline eval separately with linux commands from the command line?

siamii
  • 23,374
  • 28
  • 93
  • 143
  • 1
    Have you got `bc` installed? It's a calculator in itself, it'd probably be easier to use that than bash. – Rup May 15 '13 at 22:56
  • possible duplicate of [Bash command to sum a column of numbers](http://stackoverflow.com/questions/3096259/bash-command-to-sum-a-column-of-numbers) – dev-null-dweller May 15 '13 at 22:57
  • 2
    Useless use of cat: use `grep eval results.txt` instead. – Kevin May 15 '13 at 22:58

1 Answers1

6

Awk can do it

% grep eval results.txt | awk -F: '{a[$1]+=$2}END{for(i in a)print i ": " a[i]}'
eval: 0.141235
baseline eval (random): 0.00507132

Better yet, as Johnsyweb mentioned in a comment, let do the searching too:

awk -F: '/eval/{a[$1]+=$2}END{for(i in a)print i ": " a[i]}' results.txt
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
Kevin
  • 53,822
  • 15
  • 101
  • 132
  • well when I see your answer, I realize that, either you or me, have misunderstood the question... `sum eval and baseline eval separately`... I may be wrong... – Kent May 15 '13 at 23:04
  • `awk -F: '/eval/ {a[$1]+=$2}END{for(i in a)print i ": " a[i]}' results.txt` , surely!? – johnsyweb May 15 '13 at 23:07
  • @Kent: This ***does*** sum the two separately. – johnsyweb May 15 '13 at 23:08
  • @Johnsyweb I know what the awk line does. I understood the Q as `sum(eval and baseline eval) separately`... I could be on the wrong way... :( – Kent May 15 '13 at 23:10