Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash--instersection of two files based on a specific columns?

I want to do the following and would really appreciate if someone can help me accomplish this:

I have 2 tab-delim files named File1.txt and File2.txt(shown below). If the 2nd column (integer) in File1.txt is found in 3rd column (integer) of File2.txt then i want to make new_File1.txt and new_File2.txt and keep appending the rows.

File1.txt:

1   80  xyc
1   304 xyv
1   813653  xyb
1   813661  xyn
1   954653  xym

File2.txt:

abvb    1   178
aaa 1   304 
ttt 1   353
ggg 1   98971
ghj 1   813653

So for example my new_File1.txt will look like:

new_File1.txt:

1   304 xyv
1   813653  xyb

new_File2.txt:

aaa     1   304
ghj     1   813653

Thank you all for your help! I have used :

join -1 2 -2 3 -t '\t' file1.txt file2.txt

but it gives me the following output:

\1      80      xyc\abvb        1       178
\1      80      xyc\aaa 1       304
\1      80      xyc\ttt 1       353
\1      80      xyc\ggg 1       98971
\1      80      xyc\ghj 1       813653
\1      304     xyv\abvb        1       178
\1      304     xyv\aaa 1       304
\1      304     xyv\ttt 1       353
\1      304     xyv\ggg 1       98971
\1      304     xyv\ghj 1       813653
\1      813653  xyb\abvb        1       178
\1      813653  xyb\aaa 1       304
\1      813653  xyb\ttt 1       353
\1      813653  xyb\ggg 1       98971
\1      813653  xyb\ghj 1       813653
\1      813661  xyn\abvb        1       178
\1      813661  xyn\aaa 1       304
\1      813661  xyn\ttt 1       353
\1      813661  xyn\ggg 1       98971
\1      813661  xyn\ghj 1       813653
\1      954653  xym\abvb        1       178
\1      954653  xym\aaa 1       304
\1      954653  xym\ttt 1       353
\1      954653  xym\ggg 1       98971
\1      954653  xym\ghj 1       813653
like image 856
RnD Avatar asked Jun 20 '11 06:06

RnD


1 Answers

You're looking for join(1).

like image 197
Ignacio Vazquez-Abrams Avatar answered Sep 23 '22 13:09

Ignacio Vazquez-Abrams