Append values of duplicate entries


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append values of duplicate entries
# 1  
Old 10-27-2013
Append values of duplicate entries

My input file is:

Code:
LOC_Os01g01870    GO:0006139
LOC_Os01g01870    GO:0009058
LOC_Os01g02570    GO:0006464
LOC_Os01g02570    GO:0009987
LOC_Os01g02570    GO:0008152
LOC_Os01g04380    GO:0006950
LOC_Os01g04380    GO:0009628

I want to append the duplicate values in a tab/space separated manner:

Code:
LOC_Os01g01870    GO:0006139 GO:0009058
LOC_Os01g02570    GO:0006464 GO:0009987 GO:0008152
LOC_Os01g04380    GO:0006950 GO:0009628

Thanks..
# 2  
Old 10-27-2013
Hi, this is a fairly frequent question. If the file is sorted try:
Code:
awk 'p!=$1{if(s)print s; p=$1; s=$0; next} {s=s OFS $2} END{print s}' file


Last edited by Scrutinizer; 10-28-2013 at 02:50 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 10-28-2013
May try this...for given input this will work

Code:
$ cat file
LOC_Os01g01870    GO:0006139
LOC_Os01g01870    GO:0009058
LOC_Os01g02570    GO:0006464
LOC_Os01g02570    GO:0009987
LOC_Os01g02570    GO:0008152
LOC_Os01g04380    GO:0006950
LOC_Os01g04380    GO:0009628

Code:
$ awk '{printf (!_[$1]++)?(NR==1)?$0 FS:RS $0 FS:($1="")$0 FS}END{printf RS}' file

Resulting

Code:
LOC_Os01g01870    GO:0006139  GO:0009058 
LOC_Os01g02570    GO:0006464  GO:0009987  GO:0008152 
LOC_Os01g04380    GO:0006950  GO:0009628

This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Remove duplicate values with condition

Hi Gents, Please can you help me to get the desired output . In the first column I have some duplicate records, The condition is that all need to reject the duplicate record keeping the last occurrence. But the condition is. If the last occurrence is equal to value 14 or 98 in column 3 and... (2 Replies)
Discussion started by: jiam912
2 Replies

3. UNIX for Dummies Questions & Answers

How to append values to a string?

Hi, Requesting some help with a problem I am facing with string function in UNIX. I wish to create 2 string variables: 1st header string containing output_1, output_2, .. , output_<n> and 2nd data string containing the filename separated by colon (":") and corresponding filesize separated by... (6 Replies)
Discussion started by: vkumbhakarna
6 Replies

4. Shell Programming and Scripting

Duplicate values merge

Dear Gents, Please can you help me to solve this problem. Input file... 22057485 ,219 ,1050 22057485 ,223 ,1050 21897425 ,278 ,1050 21897425 ,279 ,1050 21897425 ,287 ,1050 20497465 ,602 ,1051 20517500 ,677 ,1051 20517500 ,681 ,1051 20577555 ,775 ,1052 20577555 ,778... (7 Replies)
Discussion started by: jiam912
7 Replies

5. Shell Programming and Scripting

Extract values of duplicate keys

I have two questions that are related, so it would be great if you can help me with both! Question1: I have a file A that looks like this: a x b y b z c w I want to get something like: a x b y; z c w Given that a,b,c has no spaces. But the other letters might contain spaces. ... (2 Replies)
Discussion started by: Viernes
2 Replies

6. Shell Programming and Scripting

duplicate values

Hi, How to enumerate duplicate values, without sorting the file. example 1 1 2 1 3 1 1 2 2 2 3 2 1 3 2 3 3 3 Where the first column have the repetead values without sorting, I would like to get the value of the times that the value is repetead , as I show... (2 Replies)
Discussion started by: jiam912
2 Replies

7. Shell Programming and Scripting

Using awk to append incremental numbers to the end of duplicate file names.

I'm currently working on a script that extracts files from a .zip, runs an sha1sum against them and then uses awk to pre-format them into zomething more readable thusly: Z 69 89e013b0d8aa2f9a79fcec4f2d71c6a469222c07 File1 Z 69 6c3aea28ce22b495e68e022a1578204a9de908ed File2 Z 69... (5 Replies)
Discussion started by: Ethereal
5 Replies

8. Shell Programming and Scripting

Cleaning up Arrays with duplicate values

Hey Gang! So I have two Arrays. @linecount and @hit. Now these arrays contain numbers which I'm using as line placeholders on files. I need these two arrays to sync up and not repeat a number. Here are the contents (spam alert) @linecount 1 28 53 86 87 88 89 90 91 92 93 94 (5 Replies)
Discussion started by: adelsin
5 Replies

9. Shell Programming and Scripting

Append values before a string

hi all, i have variables a and b with values, like a="/var/tmp/new.sh /var/tmp/new2.sh" b="/TEST" how i need to append the value "/TEST" before the values for the variable "a" so that i get the output as /TEST/var/tmp/new.sh /TEST/var/tmp/new2.sh plz help me Regards, NG (2 Replies)
Discussion started by: Nandagopal
2 Replies

10. UNIX for Dummies Questions & Answers

use awk to append values

Hi, I have a file like this: tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 tag1:value1 tag2:value2 tag3:value3 and what i want is: value1 value2 value3 value1 value2 value3 (15 Replies)
Discussion started by: nickrick
15 Replies
Login or Register to Ask a Question