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?

3
  • 1
    Have you got bc installed? It's a calculator in itself, it'd probably be easier to use that than bash. Commented May 15, 2013 at 22:56
  • possible duplicate of Bash command to sum a column of numbers Commented May 15, 2013 at 22:57
  • 2
    Useless use of cat: use grep eval results.txt instead. Commented May 15, 2013 at 22:58

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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...
awk -F: '/eval/ {a[$1]+=$2}END{for(i in a)print i ": " a[i]}' results.txt , surely!?
@Kent: This does sum the two separately.
@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... :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.