2

I have a file, which looks like:

Lorem ipsum dolor sit amet... 2465m id porttitor libero mauris at magna... 1m istique pretium tincidunt. V...1200m ... 

I want to calculate (using unix commands, awk, grep etc.) sum of trailing number, I mean (2465 + 1 + 1200 + ...) / NUMBER_OF_LINES. What is the best way to do that?

2
  • 2
    could your line look like foo...bar... foo200m? I mean non-blank char before the first digit? Commented Sep 17, 2013 at 14:22
  • No, there is always space Commented Sep 17, 2013 at 14:49

3 Answers 3

7

This makes it:

$ awk '{sum+=$NF+0} END{print "total sum is " sum " and average " sum/NR}' file total sum is 3666 and average 1222 

$NF stands for the last word of every line. It uses $NF+0 so that the trailing m and other characters are not taken into account. Then it keeps the sum in sum variable, that is printed at the end. Finally it prints the average, taken dividing the sum by NR, which has the value of number of lines processed.

Sign up to request clarification or add additional context in comments.

4 Comments

your command actually prints out an incorrect average of 822 for the OP's input.
you changed OP's example, added a space, didn't u? ;)
@dogbane 822 would be 3666/4 and I took just three first lines, as the ... would be filled with other content.
@Kent yes :) I assumed what you commented under the question
4

I would do this:

grep -oP '\d+(?=m\s*$)' file|awk '{_+=$0}END{printf "Avg: %0.2f\n",_/NR}' 

it has two processes, grep and awk, but it works the case that the number column/field starts with non-digit char. like:

foo123 456 ffffff100m xbar 222 444 bbbbb200m 234 df343 xxxxxx300m 

with the above example, it outputs :

Avg: 200.00 

with your example it outputs:

Avg: 1222.00 

1 Comment

@fedorqui thx, he didn't describe the Q well. the ... is confusing. if there were spaces, yours and konsoleboxs answer are better.
2
awk '{sum+=$NF}END{printf "Average: %0.2f\n",sum/NR}' file 

Input:

Lorem ipsum dolor sit amet... 2465m id porttitor libero mauris at magna... 1m istique pretium tincidunt. V...1200m 

Output:

Average: 822.00 

Some parts actually didn't have spaces and does not work with FS=" ." so you really had to convert the string.

awk '{t=$NF;gsub(/[^0-9]/,"",t);sum+=t}END{printf "Average: %0.2f\n",sum/NR}' 

Output:

Average: 1222.00 

7 Comments

the result is not correct, echo "(2465+1+1200)/3"|bc gives 1222 on my laptop. the big "V" in last line is the problem. your awk works only if the NF beginning with the number.
what if the last col is foo300bar300m ?
@Kent I considered that but I don't think that would be an occasion.
I agree. perhaps your initial attempt was exactly OP's looking for. he just didn't describe the question well.
Yeah I actually was the one who had the right answer for the average first :) fedorqui's solution only had it with the sum.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.