You seem to want to add the last field. This would add the last field to a variable. The END
block is executed after the input is exhausted, so sum
would be printed at the end:
tail -2 file.A | awk -F: '{sum+=$NF}END{print sum}'
Question
I have a file like this
file A :
min:353 max:685 avg:519
min:565 max:7984 avg:4274
min:278 max:5617 avg:2947
min:624 max:6768 avg:3639
min:27 max:809 avg:418
min:809 max:3685 avg:2247
min:958 max:2276 avg:1617
I trying to get last two line of avg numbers to ad them together.
Like 2247+1617
than output the value 3864
.
How can I achieve it?
So far my code is like this : (sorry for limited knowledge)
tail -n 2 file.A | awk -F '[: ]' '{print $6}'
Solution 2
You seem to want to add the last field. This would add the last field to a variable. The END
block is executed after the input is exhausted, so sum
would be printed at the end:
tail -2 file.A | awk -F: '{sum+=$NF}END{print sum}'
OTHER TIPS
Here is an awk
only
awk -F: 'FNR==NR {n=NR;next} FNR>n-2 {sum+=$NF}END{print sum}' file.A{,}
3864
Or you can just sum the two last value:
awk -F: '{f=s;s=$NF}END{print s+f}' file.A
3864
Similar approach as devnull's answer but using tac
.
tac file.A | awk -F: '{sum+=$NF}NR==2{print sum;exit}'
3864
tac
reverses the fileawk
we sum
the last column ($NF
). sum
and exit.