Key Matching using shell

前端 未结 2 1052
萌比男神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
    
    0 讨论(0)
  • 2020-12-22 13:26

    Try this:

    awk -F', ' '{a[$1]=a[$1]","$2}END{for(i in a) print i a[i]}' input.txt
    

    Output:

    Name1,Phone1,Phone2
    Name2,Phone2,Phone1
    Name3,Phone1
    Name4,Phone5,Phone1
    
    0 讨论(0)
提交回复
热议问题