group by 'last' value in bash

后端 未结 4 1293
借酒劲吻你
借酒劲吻你 2021-01-26 06:48

I have a two-column file:

1,112
1,123
2,123
2,124
2,144
3,158
4,123
4,158
5,123

I need to know last column2 value for each column1:

<         


        
4条回答
  •  耶瑟儿~
    2021-01-26 07:34

    With GNU bash:

    declare -A array   # associative array
    
    # read from file
    while IFS=, read a b; do array[$a]="$b"; done < file
    
    # print array
    for i in "${!array[@]}"; do echo "$i,${array[$i]}"; done
    

    Output:

    1,123
    2,144
    3,158
    4,158
    5,123
    

提交回复
热议问题