How to print out the unique value?

Hello All,

I have a file with below sample values.

Ben_Determination_Appeals_BPEL[1.0]
Ben_Determination_Appeals_BPEL[2.0]
Ben_Determination_Appeals_BPEL[3.0]
Terminate_SIP_Agreement_BPEL[1.0]
Terminate_SIP_Agreement_BPEL[2.0]

So i want to print those value which is having highest version with uniq name

Expected output--

Terminate_SIP_Agreement_BPEL[2.0]
Ben_Determination_Appeals_BPEL[3.0]
awk -F'[][]' '{if(A[$1]<$2) A[$1]=$2}END{for(k in A) print k"["A[k]"]"}' file
2 Likes

Thanks a lot Yoda. What if i want to delete the highest version of that name from the same list. Iam not able to find out about awk deletion.

Here an awk solution:

awk -F'[][]' '
        {
                A[++c] = $0
                if ( M[$1] < $2 )
                        M[$1] = $2
        }
        END {
                for ( i = 1; i <= c; i++ )
                {
                        n = split ( A, V, "[][]" )
                        if ( M[V[1]] != V[2] )
                                print A
                }
        }
' file

Output:

Ben_Determination_Appeals_BPEL[1.0]
Ben_Determination_Appeals_BPEL[2.0]
Terminate_SIP_Agreement_BPEL[1.0]

Thanks Yoda. Could you please explain it.

awk -F'[][]' '                                          # Set ] [ as field separators.
        {
                A[++c] = $0                             # Create indexed array: A for each record
                if ( M[$1] < $2 )                       # Create associative array: M for record indexed by 1st field and highest 2nd field value
                        M[$1] = $2
        }
        END {                                           # END Block
                for ( i = 1; i <= c; i++ )              # For i <= c (c is highest index of array: A
                {
                        n = split ( A, V, "[][]" )   # Split records in array: by field separator ][ and store it in array: V
                        if ( M[V[1]] != V[2] )          # Print array: A element only if V[2] != M[V[1]] (M[V[1]] has highest version no. )
                                print A
                }
        }
' file
1 Like

Thanks Yoda. Awk is simply very powerful. Need to learn it to know more about it.