Joining Two Files Matching Two Columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Joining Two Files Matching Two Columns
# 1  
Old 07-15-2016
Joining Two Files Matching Two Columns

Hi All,

I am looking to join two files where column 1 of file A matches with column 1 of file B and column 5 of files A matches with column 2 of file B. After joining the files based on above condition, out should contain entire line of file A and column 3, 4 and 5 of file B.

Here is sample data:


Code:
File A

7507345|NIP1|0.0.0.2|127369256|17561|Closed
3323077|NIP1|0.0.0.1|142830388|34727|Closed
3323077|NIP4|0.0.0.1|773455302|386196|Closed
8171620|NIP8|0.0.0.1|176123103157225888|Closed

File B

7507345|17561|ED|BILL|0
3323077|34727|EF|LAST_BILL_T|0

I have gone through the thread "Join two files combining multiple columns and produce mix and match output* but still not clear on this.

I will appreciate your help on this.

Thanks
Angsuman




Moderator's Comments:
Mod Comment Please use code tags, NOT icode tags for data also!

Last edited by RudiC; 07-15-2016 at 02:12 PM.. Reason: Changed ICODE tags to CODE tags.
# 2  
Old 07-15-2016
How did you apply/modify that cited thread?
# 3  
Old 07-16-2016
Hello RudiC,

I have used following comparing my requirement with example given in the thread. It did not work for me.

Here is the command:

Code:
awk -F, 'FNR==NR{A[$2 OFS $3]=FILENAME"."$1 OFS FILENAME"."$3;B[$2 OFS $3]=FILENAME"."$2;next} ($1 OFS $4 in A){print A[$1 OFS $4] OFS FILENAME"." $6 OFS FILENAME"." $1 OFS B[$1 OFS $4] OFS FILENAME"." $1}' OFS=, fileA fileB

I need help to understand the how does that command works.

Thanks
Angshuman

Last edited by Don Cragun; 07-16-2016 at 08:49 PM.. Reason: Change HTML tags to CODE tags.
# 4  
Old 07-16-2016
Did you apply that command totally unmodified? After specifying in post#1 that you want to match fields 1 and 5 of fileA with fields 1 and 2 of fileB, you are using fields 2 and 3 of fileA and fields 1 and 4 of fileB. Do you see a chance this should work? On top, the field separator of your files seems to be | , but the script uses , .

A few hints on that script: It first reads fileA and tries to assign a string composition of FILENAME and fields 1 and 3 to array A indexed by fields 2 and 3 (which fails as the fields are NOT separated due to wrong FS) and similar to array B. Then, reading fileB, it tries to match fields 1 and 4 with the indices of A (which would fail even if the FS were correct), and, on positive match, prints an even stranger composition of array elements, FILENAMEs, and fields. Like
Code:
file1.7507345|file1.0.0.0.2|file2.|file2.7507345||file2.7507345
file1.3323077|file1.0.0.0.1|file2.|file2.3323077||file2.3323077

(after correcting for the worst errors)

Last edited by RudiC; 07-16-2016 at 11:02 AM..
# 5  
Old 07-16-2016
Hello RudiC,

I modified the script as per my requirement but it failed to give output. I did not understand the part "tries to assign a string composition of FILENAME and fields 1 and 3 to array A indexed by by fields 2 and 3". Here is my date and requirement:

Code:
File A

7507345|NIP1|0.0.0.2|127369256|17561|Closed
3323077|NIP1|0.0.0.1|142830388|34727|Closed
3323077|NIP4|0.0.0.1|773455302|386196|Closed
8171620|NIP8|0.0.0.1|176123103157225888|Closed

File B

7507345|17561|ED|BILL|0
3323077|34727|EF|LAST_BILL_T|0

Expected Output:

7507345|NIP1|0.0.0.2|127369256|17561|ED|BILL|0
3323077|NIP1|0.0.0.1|142830388|34727|EF|LAST_BILL_T|0

I will really appreciate if you can suggest.

Thanks
Angshuman
# 6  
Old 07-17-2016
Please show your modified script.
# 7  
Old 07-17-2016
join using join command

why not use the join command to join files? The problem is it only can join on 1 field in each file (default the first field in each file). But after the first join it gets much easier.
Code:
$ join -t \| a.tmp b.tmp

7507345|NIP1|0.0.0.2|127369256|17561|Closed|17561|ED|BILL|0
3323077|NIP1|0.0.0.1|142830388|34727|Closed|34727|EF|LAST_BILL_T|0
3323077|NIP4|0.0.0.1|773455302|386196|Closed|34727|EF|LAST_BILL_T|0

After this you only have to keep the lines where field 5 equals field 7, and this is easy with awk. So:
Code:
$ join -t \| a.tmp b.tmp |awk -F\| 'BEGIN {OFS="|"}; $5 == $7 {print $1,$2, $3, $4, $5, $6, $8, $9, $10}'

7507345|NIP1|0.0.0.2|127369256|17561|Closed|ED|BILL|0
3323077|NIP1|0.0.0.1|142830388|34727|Closed|EF|LAST_BILL_T|0

Note: join requires the two files to be sorted on the key fields!
This User Gave Thanks to Ivo Breeden 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

Joining files using awk not extracting all columns from File 2

Hello All I'm joining two files using Awk by Left outer join on the file 1 File 1 1 AA 2 BB 3 CC 4 DD File 2 1 IND 100 200 300 2 AUS 400 500 600 5 USA 700 800 900 (18 Replies)
Discussion started by: venkat_reddy
18 Replies

2. UNIX for Dummies Questions & Answers

Joining different columns from multiple files

Hello again, I am trying to join 3rd column of 3 files into the end on one file and save it separately... my data looks like this file 1 Bob, Green, 80 Mark, Brown, 70 Tina, Smith, 60 file 2 Bob, Green, 70 Mark, Brown, 60 Tina, Smith, 50 file 3 Bob, Green, 50 Mark, Brown,60 Tina,... (6 Replies)
Discussion started by: A-V
6 Replies

3. Shell Programming and Scripting

Join two files with matching columns

Hi, I need to join two files together with one common value in a column. I think I can use awk or join or a combination but I can't quite get it. Basically my data looks like this, with the TICKER columns matching up in each file File1 TICKER,column 1, column, 2, column, 3, column 4 ... (6 Replies)
Discussion started by: unkleruckus
6 Replies

4. Shell Programming and Scripting

Help with awk Matching columns from two files

Hello, I have two files as following: #bin chrom chromStart chromEnd name score strand observed 585 chr2 29442 29443 rs4637157 0 + C/T 585 chr2 33011 33012 rs13423995 0 + A/G 585 chr2 34502 34503 rs13386087 0 + ... (2 Replies)
Discussion started by: Homa
2 Replies

5. Shell Programming and Scripting

Other alternative for joining together columns from multiple files

Hi again, I have monthly one-column files of roughly around 10 years. Is there a more efficient way to concatenate these files column-wise other than using paste command? For instance: file1.txt 12 13 15 12 file2.txt 14 15 18 19 file3.txt 20 21 (8 Replies)
Discussion started by: ida1215
8 Replies

6. UNIX for Dummies Questions & Answers

Matching corresponding columns in two different files

Hi to all, I have two separated files: FILE1 "V1" "V2" "V3" Mary James Nicole Robert Francisco Sophie Nancy Antony Matt Josephine Louise Rose Mark Simon Charles FILE2 "V1" "V2" "V3"... (2 Replies)
Discussion started by: eleonoral
2 Replies

7. Shell Programming and Scripting

NR==FNR trick for joining columns from two files

foo.txt 1 rs2887286 0 1145994 C T 1 rs1240743 0 1323299 C A 1 rs1695824 0 1355433 G T 1 rs3766180 0 1468016 G A 1 rs7519837 0 1500664 A G 1 rs2272908 0 ... (12 Replies)
Discussion started by: genehunter
12 Replies

8. Shell Programming and Scripting

matching columns from two files

Hey, I have two files that have exactly the same format. They are both tab-delimited and contain 12 columns. However the # of rows vary. What I want to do is match columns # 5,6 and 7 between the two files. If they do match exactly (based on numbers) then I want the whole row from file 2 to... (1 Reply)
Discussion started by: phil_heath
1 Replies

9. Shell Programming and Scripting

Joining two files based on columns/fields

I've got two files, File1 and File2 File 1 has got combination of col1, col2 and col3 which comes on file2 as well, file2 does not get col4. Now based on col1, col2 and col3, I would like to get col4 from file1 and all the columns from file2 in a new file Any ideas? File1 ------ Col1 col2... (11 Replies)
Discussion started by: rudoraj
11 Replies

10. Shell Programming and Scripting

Joining columns from two files, if the key matches

I am trying to join/paste columns from two files for the rows with matching first field. Any help will be appreciated. Files can not be sorted and may not have all rows in both files. Thanks. File1 aaa 111 bbb 222 ccc 333 File2 aaa sss mmmm ccc kkkk llll ddd xxx yyy Want to... (1 Reply)
Discussion started by: sk_sd
1 Replies
Login or Register to Ask a Question