For a given and unique string how to find all corresponding strings

47 views Asked by At
# cat lun.txt
/dev/sdb 60002ac00000000000000f0f0000799d
/dev/sdc 60002ac00000000000000fdb0000799d
/dev/sdd 60002ac00000000000000f0f0000799d
/dev/sde 60002ac00000000000000fdb0000799d
#

How do I list all devices for a given WWID, like below:

60002ac00000000000000f0f0000799d 
    /dev/sdb
    /dev/sdd
60002ac00000000000000fdb0000799d
    /dev/sdc
    /dev/sde

I have tried using sort, uniq and awk. But no luck

2

There are 2 answers

0
nu11p01n73R On BEST ANSWER

How about something like

$ awk '{ device[$2]=device[$2]"\n"$1 } END{ for(i in device) print i,device[i] }' lun.txt
60002ac00000000000000fdb0000799d 
/dev/sdc
/dev/sde
60002ac00000000000000f0f0000799d 
/dev/sdb
/dev/sdd
  • device[$2]=device[$2]"\n"$1 creates an array device indexed by the wwid, $2 appends the $1, first column with the array element

  • for(i in device) print i,device[i] prints the content of the array

0
Susanta Dutta On

I just added a tab(\t) to get the desired output.

awk '{ device[$2]=device[$2]"\n\t"$1 } END{ for(i in device) print i,device[i] }' lun.txt
60002ac00000000000000fdb0000799d
        /dev/sdc
        /dev/sde
60002ac00000000000000f0f0000799d
        /dev/sdb
        /dev/sdd