I have a unsorted list of integers; say
1
2
3
3
4
5
5
2
I want to sort the list, and then calculate cumulative sum until the sum reaches a pre-defined integer; say 17. After stopping, it should print the last integer which added up to exceed the pre-defined integer. See example
My sorted list will be:
5
5
4
3
3
2
2
1
5+5+4+3=17, so the cumulative sum will stop here and will print the last integer which is '3'
I tried this with no luck :(
awk 'BEGIN {sum=0} {sum= sum+$0; if (sum >= '$pre-defined_integer') print $0}