Key Matching using shell

前端 未结 2 1050
萌比男神i
萌比男神i 2020-12-22 12:44

I wanted to see different type of answers I receive from you guys for the below problem. I am curious to see below problem being solved completely through array or any other

2条回答
  •  情书的邮戳
    2020-12-22 13:08

    With bash and sort:

    #!/bin/bash
    
    declare -A array         # define associative array
    
    # read file input.txt to array
    while IFS=", " read -r line number; do
      array["$line"]+=",$number"
    done < input.txt
    
    # print array
    for i in "${!array[@]}"; do
      echo "$i${array[$i]}"
    done | sort 
    

    Output:

    Name1,Phone1,Phone2
    Name2,Phone2,Phone1
    Name3,Phone1
    Name4,Phone5,Phone1
    

提交回复
热议问题