have to retrieve the distinct values (not duplicate) from 2nd column and display


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting have to retrieve the distinct values (not duplicate) from 2nd column and display
# 1  
Old 03-09-2010
have to retrieve the distinct values (not duplicate) from 2nd column and display

I have a text file names test2 with 3 columns as below . We have to retrieve the distinct values (not duplicate) from 2nd column and display. I have used the below command but giving some error.



Code:
NS3303 NS CRAFT LTD
NS3303 NS CHIRON VACCINES LTD
NS3303 NS ALLIED MEDICARE LTD
NS3303 NS BODET LTD
NS3303 LW FACTORY SHACK LTD
NS3303 NS ARC NEWSON LTD
NS3303 NS COASTAL MANAGEMENT LTD
NS3303 NS BIGWOOD ASSOCIATES LTD
NS3303 LW CENTRAL COMMUNICATION PRODUCTS LTD
NS3303 Lw WHITEHEAD GROUP LTD
NS3303 NS STUDIO BOX LTD
NS3303 EA PETAL POSTFORMING LTD
NS3303 LW ERSKINE HOSPITAL
NS3303 NS FRUDD CONSTRUCTION LTD
NS3303 EA ELWELL WATCHORN & SAXTON

Code:
awk -F, unique | awk -F, '{print $2}' test2

Code:
The output should be 
NS
LW
EA

Can you please help me? J

Last edited by radoulov; 03-09-2010 at 08:30 AM.. Reason: Please use code tags!
# 2  
Old 03-09-2010
Code:
awk '{up=toupper($2);a[up]}END{for(i in a) print i}' infile

# 3  
Old 03-09-2010
display the whole line
Code:
awk '![arr[$2]++' inputfile

display column 2
Code:
awk '![arr[$2]++ {print $2}' inputfile


Last edited by jim mcnamara; 03-09-2010 at 10:18 AM.. Reason: wrong answer...
# 4  
Old 03-09-2010
Is this a typo?

Code:
Lw

# 5  
Old 03-09-2010
Quote:
Originally Posted by jim mcnamara
Code:
awk '![arr[$2]++' inputfile

shouldn't it be like this though the OP wanted the second column only...
Code:
awk '!arr[$2]++' inputfile

# 6  
Old 03-09-2010
bash:
Code:
cut -d' ' -f2 test2 | sort -u

# 7  
Old 03-10-2010
Hi malcomex999

Hi malcomex999,

Thank u soo much for your help.but when I used this command , I am getting the output only NS .can you please help me further?

But my output shouls display all distinct values in the 2nd column means

Code:
NS
LW
EA

Code:
awk '{up=toupper($2);a[up]}END{for(i in a) print i}' infile


Last edited by radoulov; 03-10-2010 at 03:34 AM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find lines with duplicate values in a particular column

I have a file with 5 columns. I want to pull out all records where the value in column 4 is not unique. For example in the sample below, I would want it to print out all lines except for the last two. 40991764 2419 724 47182 Cand A 40992936 3591 724 47182 Cand B 40993016 3671 724 47182 Cand C... (5 Replies)
Discussion started by: kaktus
5 Replies

2. Shell Programming and Scripting

Find duplicate values in specific column and delete all the duplicate values

Dear folks I have a map file of around 54K lines and some of the values in the second column have the same value and I want to find them and delete all of the same values. I looked over duplicate commands but my case is not to keep one of the duplicate values. I want to remove all of the same... (4 Replies)
Discussion started by: sajmar
4 Replies

3. Shell Programming and Scripting

Remove duplicate values in a column(not in the file)

Hi Gurus, I have a file(weblog) as below abc|xyz|123|agentcode=sample code abcdeeess,agentcode=sample code abcdeeess,agentcode=sample code abcdeeess|agentadd=abcd stereet 23343,agentadd=abcd stereet 23343 sss|wwq|999|agentcode=sample1 code wqwdeeess,gentcode=sample1 code... (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

4. Shell Programming and Scripting

Filter file to remove duplicate values in first column

Hello, I have a script that is generating a tab delimited output file. num Name PCA_A1 PCA_A2 PCA_A3 0 compound_00 -3.5054 -1.1207 -2.4372 1 compound_01 -2.2641 0.4287 -1.6120 3 compound_03 -1.3053 1.8495 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

5. Shell Programming and Scripting

Identify duplicate values at first column in csv file

Input 1,ABCD,no 2,system,yes 3,ABCD,yes 4,XYZ,no 5,XYZ,yes 6,pc,noCode used to find duplicate with regard to 2nd column awk 'NR == 1 {p=$2; next} p == $2 { print "Line" NR "$2 is duplicated"} {p=$2}' FS="," ./input.csv Now is there a wise way to de-duplicate the entire line (remove... (4 Replies)
Discussion started by: deadyetagain
4 Replies

6. Shell Programming and Scripting

Splitting the numeric vs alpha values in a column to distinct columns

How could i take an input file and split the numeric values from the alpha values (123 vs abc) to distinc columns, and if the source is blank to keep it blank (null) in both of the new columns: So if the source file had a column like: Value: |1 | |2.3| | | |No| I would... (7 Replies)
Discussion started by: driftlogic
7 Replies

7. Shell Programming and Scripting

Get the average from column, and eliminate the duplicate values.

Dear Experts, Kindly help me please, I have a big file where there is duplicate values in col 11 till col 23, every 2 rows appers a new numbers, but in each row there is different coordinates x and y in col 57 till col 74. Please i will like to get a single value and average of the x and y... (8 Replies)
Discussion started by: jiam912
8 Replies

8. UNIX for Dummies Questions & Answers

[SOLVED] remove lines that have duplicate values in column two

Hi, I've got a file that I'd like to uniquely sort based on column 2 (values in column 2 begin with "comp"). I tried sort -t -nuk2,3 file.txtBut got: sort: multi-character tab `-nuk2,3' "man sort" did not help me out Any pointers? Input: Output: (5 Replies)
Discussion started by: pathunkathunk
5 Replies

9. UNIX for Dummies Questions & Answers

count number of distinct values in each column with awk

Hi ! input: A|B|C|D A|F|C|E A|B|I|C A|T|I|B As the title of the thread says, I would need to get: 1|3|2|4 I tried different variants of this command, but I don't manage to obtain what I need: gawk 'BEGIN{FS=OFS="|"}{for(i=1; i<=NF; i++) a++} END {for (b in a) print b}' input ... (2 Replies)
Discussion started by: beca123456
2 Replies

10. Shell Programming and Scripting

Find and replace duplicate column values in a row

I have file which as 12 columns and values like this 1,2,3,4,5 a,b,c,d,e b,c,a,e,f a,b,e,a,h if you see the first column has duplicate values, I need to identify (print it to console) the duplicate value (which is 'a') and also remove duplicate values like below. I could be in two... (5 Replies)
Discussion started by: nuthalapati
5 Replies
Login or Register to Ask a Question