Merge Two Tables with duplicates in first table


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge Two Tables with duplicates in first table
# 1  
Old 05-16-2011
Merge Two Tables with duplicates in first table

Hi..

File 1:

Code:
1 aa rep
1 dd rep
1 kk rep
2 bb sad
2 ss sad
3 ee dam

File 2

Code:
1 apple  fruit
2 mango tree
3 lilly flower

output:

Code:
1 aaple fruit aa,dd,kk rep
2 mango tree bb,ss sad
3 lilly flower ee dam

How can i do this in either awk, perl or unix.

Last edited by radoulov; 05-16-2011 at 03:56 PM.. Reason: Code tags, please!
# 2  
Old 05-16-2011
Code:
awk 'NR == FNR {
  k[$1] = k[$1] ? k[$1] "," $2 : $2
  l[$1] = $3; next
  }
{
  print $0, k[$1], l[$1]
  }' file1 file2

# 3  
Old 05-16-2011
the code is working, but when it is printing, the columns from first file i.e the concatenated ones are printing in second line..
# 4  
Old 05-16-2011
I get this output:

Code:
zsh-4.3.11[t]% head file[12]
==> file1 <==
1 aa rep
1 dd rep
1 kk rep
2 bb sad
2 ss sad
3 ee dam

==> file2 <==
1 apple  fruit
2 mango tree
3 lilly flower
zsh-4.3.11[t]% awk 'NR == FNR {
  k[$1] = k[$1] ? k[$1] "," $2 : $2
  l[$1] = $3; next
  }
{
  print $0, k[$1], l[$1]
  }' file1 file2
1 apple  fruit aa,dd,kk rep
2 mango tree bb,ss sad
3 lilly flower ee dam

Is it correct? Do you get a different one?
This User Gave Thanks to radoulov For This Post:
# 5  
Old 05-16-2011
I am getting it like this..

Code:
1 apple  fruit 
aa,dd,kk rep
2 mango 
tree bb,ss sad
3 lilly 
flower ee dam

# 6  
Old 05-17-2011
How about this,
Code:
awk 'NR==FNR{a[$1]=a[$1]","$2;b[$1]=$3;next} b[$1] {print $0,a[$1],b[$1]}' file1 file2

This User Gave Thanks to pravin27 For This Post:
# 7  
Old 05-17-2011
Quote:
Originally Posted by empyrean
I am getting it like this..

Code:
1 apple  fruit 
aa,dd,kk rep
2 mango 
tree bb,ss sad
3 lilly 
flower ee dam

Could you post the output of the following command:

Code:
od -bc file2 | head

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

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filtering duplicates based on lookup table and rules

please help solving the following. I have access to redhat linux cluster having 32gigs of ram. I have duplicate ids for variable names, in the file 1,2 are duplicates;3,4 and 5 are duplicates;6 and 7 are duplicates. My objective is to use only the first occurrence of these duplicates. Lookup... (4 Replies)
Discussion started by: ritakadm
4 Replies

2. UNIX for Dummies Questions & Answers

How to merge two tables based on a matched column?

Hi, Please excuse me , i have searched unix forum, i am unable to find what i expect , my query is , i have 2 files of same structure and having 1 similar field/column , i need to merge 2 tables/files based on the one matched field/column (that is field 1), file 1:... (5 Replies)
Discussion started by: karthikram
5 Replies

3. Shell Programming and Scripting

Help with merge and remove duplicates

Hi all, I need some help to remove duplicates from a file before merging. I have got 2 files: file1 has data in format 4300 23456 4301 2357 the 4 byte values on the right hand side is uniq, and are not repeated anywhere in the file file 2 has data in same format but is not in... (10 Replies)
Discussion started by: roy121
10 Replies

4. Shell Programming and Scripting

Merge files without duplicates

Hi all, In a directory of many files, I need to merge only files which do not have identical lines and also the resulatant merge file should not be more than 50000 lines. Basically I need to cover up all text files in that directory and turn them to Merge files.txt with 50000 lines each ... (2 Replies)
Discussion started by: pravfraz
2 Replies

5. Shell Programming and Scripting

Find duplicates in column 1 and merge their lines (awk?)

Hi, I have a file (sorted by sort) with 8 tab delimited columns. The first column contains duplicated fields and I need to merge all these identical lines. My input file: comp100002 aaa bbb ccc ddd eee fff ggg comp100003 aba aba aba aba aba aba aba comp100003 fff fff fff fff fff fff fff... (5 Replies)
Discussion started by: falcox
5 Replies

6. Shell Programming and Scripting

Merge multiple tables into big matrix

Hi all, I have a complex (beyond my biological expertise) problem at hand. I need to merge multiple files into 1 big matrix. Please help me with some code. Inp1 Ang_0 chr1 98 T A Ang_0 chr1 352 G A Ang_0 chr1 425 C T Ang_0 chr2 ... (1 Reply)
Discussion started by: newbie83
1 Replies

7. Shell Programming and Scripting

merge multiple tables with perl

Hi everyone, I once again got stuck with merging tables and was wondering if someone could help me out on that problem. I have a number of tab delimited tables which I need to merge into one big one. All tables have the same header but a different number of rows (this could be changed if... (6 Replies)
Discussion started by: TuAd
6 Replies

8. Shell Programming and Scripting

squeeze duplicates from a table

I have files with an x amounts of rows with each row having 2 columns seperated by delimiter "|" . File contains following records for example. 15|69 15|70 15|71 15|72 15|73 15|74 16|2 16|3 16|4 16|5 16|6 16|7 16|8 16|9 16|10 16|11 16|12 (4 Replies)
Discussion started by: Alex_P
4 Replies
Login or Register to Ask a Question