How to print out the unique value?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print out the unique value?
# 1  
Old 05-17-2013
How to print out the unique value?

Hello All,

I have a file with below sample values.

Code:
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--

Code:
Terminate_SIP_Agreement_BPEL[2.0]
Ben_Determination_Appeals_BPEL[3.0]


Last edited by Vikram_Tanwar12; 05-17-2013 at 07:39 AM..
# 2  
Old 05-17-2013
Code:
awk -F'[][]' '{if(A[$1]<$2) A[$1]=$2}END{for(k in A) print k"["A[k]"]"}' file

These 2 Users Gave Thanks to Yoda For This Post:
# 3  
Old 05-20-2013
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.
# 4  
Old 05-20-2013
Here an awk solution:
Code:
awk -F'[][]' '
        {
                A[++c] = $0
                if ( M[$1] < $2 )
                        M[$1] = $2
        }
        END {
                for ( i = 1; i <= c; i++ )
                {
                        n = split ( A[i], V, "[][]" )
                        if ( M[V[1]] != V[2] )
                                print A[i]
                }
        }
' file

Output:
Code:
Ben_Determination_Appeals_BPEL[1.0]
Ben_Determination_Appeals_BPEL[2.0]
Terminate_SIP_Agreement_BPEL[1.0]

# 5  
Old 05-20-2013
Thanks Yoda. Could you please explain it.
# 6  
Old 05-20-2013
Code:
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[i], 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[i]
                }
        }
' file

This User Gave Thanks to Yoda For This Post:
# 7  
Old 05-21-2013
Thanks Yoda. Awk is simply very powerful. Need to learn it to know more about it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue using awk to print min values for unique ids

I am using the following script to search for and print minimum values for each individual Fields (3-14) for each unique id (Field 1). But when the field contains a "-99.99" ( I am ignoring "-99.99") and when the minimum value is the first line of a new id (Field 1), the output does not print Field... (13 Replies)
Discussion started by: ncwxpanther
13 Replies

2. Shell Programming and Scripting

Print count of unique values

Hello experts, I am converting a number into its binary output as : read n echo "obase=2;$n" | bc I wish to count the maximum continuous occurrences of the digit 1. Example : 1. The binary equivalent of 5 = 101. Hence the output must be 1. 2. The binary... (3 Replies)
Discussion started by: H squared
3 Replies

3. Shell Programming and Scripting

awk to print unique text in field before hyphen

Trying to print the unique values in $2 before the -, currently the count is displayed. Hopefully, the below is close. Thank you :). file chr2:46603668-46603902 EPAS1-902|gc=54.3 253.1 chr2:211471445-211471675 CPS1-1205|gc=48.3 264.7 chr19:15291762-15291983 NOTCH3-1003|gc=68.8 195.8... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk to print unique text in field

I am trying to use awk to print the unique entries in $2 So in the example below there are 3 lines but 2 of the lines match in $2 so only one is used in the output. File.txt chr17:29667512-29667673 NF1:exon.1;NF1:exon.2;NF1:exon.38;NF1:exon.4;NF1:exon.46;NF1:exon.47 703.807... (5 Replies)
Discussion started by: cmccabe
5 Replies

5. Shell Programming and Scripting

Print lines in which value in specified Col is NOT unique

Hi everyone, I have the following file, which is a 3 column tab-delineated. cat big 24 cat small 13 cat red 63 dog big 34 chicken plays 39 fish red 294 I would like to print only those lines, in which the value in Col2 is repeated. Thus, given the above input file, the desired... (7 Replies)
Discussion started by: owwow14
7 Replies

6. UNIX for Dummies Questions & Answers

Print unique lines without sort or unique

I would like to print unique lines without sort or unique. Unfortunately the server I am working on does not have sort or unique. I have not been able to contact the administrator of the server to ask him to add it for several weeks. (7 Replies)
Discussion started by: cokedude
7 Replies

7. Shell Programming and Scripting

Print unique names in a specific column using awk

Is it possible to modify file like this. 1. Remove all the duplicate names in a define column i.e 4th col 2. Count the no.of unique names separated by ";" and print as a 5th col thanx in advance!! Q input c1 30 3 Eh2 c10 96 3 Frp c41 396 3 Ua5;Lop;Kol;Kol c62 2 30 Fmp;Fmp;Fmp ... (5 Replies)
Discussion started by: quincyjones
5 Replies

8. Shell Programming and Scripting

Print unique names in each row of a specific column using awk

Is it possible to remove redundant names in the 4th column? input cqWE 100 200 singapore;singapore AZO 300 400 brazil;america;germany;ireland;germany .... .... output cqWE 100 200 singapore AZO 300 400 brazil;america;germany;ireland (4 Replies)
Discussion started by: quincyjones
4 Replies

9. Shell Programming and Scripting

Print unique records in 2 columns using awk

Is it possible to print the records that has only 1 value in 2nd column. Ex: input awex1 1 awex1 2 awex1 3 assww 1 ader34 1 ader34 2 output assww 1 (5 Replies)
Discussion started by: quincyjones
5 Replies

10. Shell Programming and Scripting

Compare multiple files and print unique lines

Hi friends, I have multiple files. For now, let's say I have two of the following style cat 1.txt cat 2.txt output.txt Please note that my files are not sorted and in the output file I need another extra column that says the file from which it is coming. I have more than 100... (19 Replies)
Discussion started by: jacobs.smith
19 Replies
Login or Register to Ask a Question