Does a way exist to find the min and max in...
Solved
Swiss Knight
Posted messages
2257
Status
Member
-
Swiss Knight Posted messages 2257 Status Member -
Swiss Knight Posted messages 2257 Status Member -
Hello!
Is there a simple way in bash to find the minimum and maximum of numbers contained in a string?
Example:
$ A=78,99,12
A trick that returns 12 as minimum and 99 as maximum?
Better: a trick that returns the average of the min found and the max found, i.e. here (99+12)/2
rounded to the nearest integer (well, in this case it will be the next higher integer, but if we divided by 3 it could be different)
I couldn't find a function like that:
min(A)=12
max(A)=99
not even in bc.
Thank you very much!
--
"If you can't explain a concept to a six-year-old, you don't understand it completely." -A. Einstein-
Is there a simple way in bash to find the minimum and maximum of numbers contained in a string?
Example:
$ A=78,99,12
A trick that returns 12 as minimum and 99 as maximum?
Better: a trick that returns the average of the min found and the max found, i.e. here (99+12)/2
rounded to the nearest integer (well, in this case it will be the next higher integer, but if we divided by 3 it could be different)
I couldn't find a function like that:
min(A)=12
max(A)=99
not even in bc.
Thank you very much!
--
"If you can't explain a concept to a six-year-old, you don't understand it completely." -A. Einstein-
3 answers
-
hello
awk can do$ awk '{n=split($0, a, "," ); asort(a); print int((a[1]+a[n])/2+.5)}' <<<"78,99,12" 56 $ -
Hi,
bash can dofor i in ${var//,/ } do min=$((min?(min>i?i:min):i)) max=$((max?(max<i?i:max):i)) done echo "min: $min - max: $max - moy: $(((max+min)/2))" min: 12 - max: 99 - moy: 55 -
:-)
Both of your solutions work very well. I still have a lot to learn.
Thank you!
"If you can't explain a concept to a six-year-old, you don't understand it fully." -A. Einstein-