Merging Tables by a column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Merging Tables by a column
# 1  
Old 08-06-2010
Merging Tables by a column

Dear Friends,

I really do not know Linux and I really would like to understand it because it does help to work with large data. I am reading this forum for 1 week to try a solution for my problem. I think that, using others post informations, I was almost there...

I have 2 big tables like this
Code:
FILE 1          FILE 2
ABC  011        ABC 012
DEF  021        XYZ o42

And I would like a final FILE 3 as
Code:
       FILE 1    FILE 2
ABC    011       012
DEF    021         0
XYZ     0         042

I tried using this code:
Code:
#!/bin/bash
nawk 'NR==FNR {
if ($1 !~ /^963/)
{
t[$1]=$2
m[$1]=$0
}
}
NR!=FNR{
if ($1 !~ /^963/)
{
if (t[$1]!="")
t[$1]=sprintf("%s %s",$0,t[$1])
else
t[$1]=sprintf("%s 0",$0)
}
}
END{
for (i in t)
if(index(t[i]," ")!=0)
        print t[i]
else
        print m[i]
}' $2 $1

But the results for that exemple is:

ABC 011 012 --> OK
DEF 021 0 --> ok
XYZ 042 --> X : when the FILE 1 do not have the word at FILE 2, it do not put 0 and atthe other column the number.

This is the fist double. The second is how can I write the nameof which file at the top of the column?

Thank you all very much!

Best Regards, Leandro

Last edited by Scott; 08-06-2010 at 04:51 PM.. Reason: Please use code tags
# 2  
Old 08-06-2010
try this:
Code:
awk 'BEGIN {print "Col A  File 1  File 2"}
     FILENAME=="file1" {arr[$1]=$2}
     FILENAME=="file2" {
       if($1 in arr) {
          # handle those in both files
          print $1, arr[ $1 ], $2; 
          delete arr[$1]
       }
       else { 
          # handle those not in file1
          print $1, "0", $2
       }     
     } 
     # handle those not in file2
     END {for (i in arr) {print i, arr[i], 0 }}'   file1  file2 > newfile


Last edited by jim mcnamara; 08-06-2010 at 06:16 PM..
# 3  
Old 08-06-2010
Dear jim mcnamara,

Thank you for your help!!

However the result I found was this:

Col A File 1 File 2
ABC 012
XYZ 0 042
0
DEF 021 0

It is great that the name is at the top!
The problem is when the column is in both file and the script just write the value from file 2. And the 0 as one line, why is this?

Best Regards, Leandro
# 4  
Old 08-06-2010
I edited the file (red) there was a typo. My bad.
# 5  
Old 08-06-2010
Hey!! It works!!! Very nice!

Col A File 1 File 2
ABC 011 012
XYZ 0 042
DEF 021 0


Thank you very, very much!

Best regards, Leandro
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merging rows based on same ID in First column.

Hellow, I have a tab-delimited file with 3 columns : BINPACKER.13259.1.p2 SSF48239 BINPACKER.13259.1.p2 PF13243 BINPACKER.13259.1.p2 G3DSA:1.50.10.20 BINPACKER.13259.2.p2 SSF48239 BINPACKER.13259.2.p2 PF13243 BINPACKER.13259.2.p2 G3DSA:1.50.10.20... (7 Replies)
Discussion started by: anjaliANJALI
7 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

Merging two tables including multiple ocurrence of column identifiers and unique lines

I would like to merge two tables based on column 1: File 1: 1 today 1 green 2 tomorrow 3 red File 2: 1 a lot 1 sometimes 2 at work 2 at home 2 sometimes 3 new 4 a lot 5 sometimes 6 at work (4 Replies)
Discussion started by: BSP
4 Replies

4. UNIX for Dummies Questions & Answers

Merging tables: identifiying common and unique elements

Hi all, I know how to merge two tables and to remove the duplicated lines based on a field (Column 2) . My next challenge is to be able to identify in a new column those common elements between table A & B, those elements in table A not present in table B and vice versa. A simple count would be... (6 Replies)
Discussion started by: lsantome
6 Replies

5. UNIX for Dummies Questions & Answers

Cut from tables based on column values

Hello, I have a tab-delimited table that may contain 11,12 or 13 columns. Depending on the number of columns, I want to cut and get a sub table as shown below. However, the awk commands in the code seem to be an issue. What should I be doing differently? #cut columns 1-2,4-5,11 when 12 &... (3 Replies)
Discussion started by: Gussifinknottle
3 Replies

6. Shell Programming and Scripting

Merging rows with same column 1 value

I have the following space-delimited input: 1 11.785710 117.857100 1 15 150 1 20 200 1 25 250 3 2.142855 21.428550 3 25 250 22 1.071435 10.714350 The first field is the ID number, the second field is the percentage of the total points that the person has and the third column is the number... (3 Replies)
Discussion started by: mdlloyd7
3 Replies

7. Shell Programming and Scripting

File merging using first column as the ref

I had two files 1.txt 2.txt. I want a 3rd file(o/p) 3.txt like below based on the common elements from the first coulmns of 1.txt and 2.txt. 1.txt 11 12 13 14 15 16 17 18 19 20 21 2.txt (6 Replies)
Discussion started by: p_sai_ias
6 Replies

8. Shell Programming and Scripting

merging two files based on first column

I had two files file1 and file2. I want a o/p file(file3) like below using first column as ref. Pls give suggestion ass join is not working as the number of lines in each file is nealry 5 C? file1 --------------------- 404000324810001 Y 404000324810004 N 404000324810008 Y 404000324810009 N... (1 Reply)
Discussion started by: p_sai_ias
1 Replies

9. Shell Programming and Scripting

awk command - column merging

I have two files having 3 coulms and 1 column respectively file1.txt 0 22.89 35.60 10 22.80 35.61 20 22.70 35.63 30 22.32 35.68 50 19.23 35.79 75 16.10 35.59 100 15.00 35.52 125 14.45 35.46 150 13.91 35.41 200 12.94 35.28 ... (7 Replies)
Discussion started by: shashi792
7 Replies

10. Shell Programming and Scripting

Merging column files

Hi,Iam new to Unix.I have a file FileA which is a variable length file where each column is seperated by delimitter "|". FileA: SrNo Name Address 1-234|name1|Addr1 1-34|name2|Addr2 1-2345|name3|Addr3 FileB: SrNo Address 1-34<<06 SPACES>>Addr1<<8 spaces>> 1-234<<05... (1 Reply)
Discussion started by: swapna321
1 Replies
Login or Register to Ask a Question