I have this array:
array=(1 2 3 4 4 3 4 3)
I can get the largest number with:
echo "num: $(printf "%d\n" ${array[@]} | sort -nr | head -n 1)"
#outputs 4
But i want to get all 4’s add sum them up, meaning I want it to output 12 (there are 3 occurrences of 4) instead. any ideas?
Using awk:
Using sort, the numbers are sorted(-n) in reverse(-r) order, and the awk keeps summing the numbers till it finds a number which is different from the previous one.