# !/bin/bash
declare -A count min
declare -i max_count=0 max_value
# loop 1: get the count and the minimum value for each B
# and find the max value for A
{
read header
while IFS=, read -r a b; do
((count[$b]++))
((count[$b] > max_count)) && max_count=${count[$b]}
if [[ ! -v min[$b] ]] || ((a < min[$b])); then min[$b]=$a; fi
if [[ -z $max_value ]] || ((max_value < a)); then max_value=$a; fi
done
} < file.csv
# loop 2: find the B's with the max count
declare -a candidates
for c in "${!count[@]}"; do
if ((count[$c] == max_count)); then
candidates+=("$c")
fi
done
# loop 3: find the minimum A value among the candidates
min_value=$max_value
for c in "${candidates[@]}"; do
((min[$c] < min_value)) && min_value=${min[$c]}
done
# loop 4: print out the candidates with the minimum A value
for c in "${candidates[@]}"; do
((min[$c] == min_value)) && echo "$c"
done
1条答案
按热度按时间nzrxty8p1#