Intersect of two columns in two separate files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Intersect of two columns in two separate files
# 1  
Old 07-15-2013
Intersect of two columns in two separate files

Hi,
I have a file like this:
Code:
abc
def
ghi
jkl
mno

My second file is like this (tab delimited):
Code:
adsad	sdfsdf	dfdf
wads	abc	dfdsf
sdsf	jkl	sfsdf
dsfds	sdfd	reor
zxczd	dsf	sff

Now, I want the code to report the lines (from file2) which have common strings in column 2 with the first file. So the output should be like this:
Code:
wads	abc	dfdsf
sdsf	jkl	sfsdf

Could anybody please help?

Thanks
# 2  
Old 07-15-2013
What have you tried so far? Share your thoughts on solving this problem.
# 3  
Old 07-15-2013
I don't know much about unix because it's not my field of study. I know that this code returns the common strings between the two files but have no idea how to manipulate it for my purpose.
Code:
awk 'NR==FNR{a[$0]=$0;next}a[$0]' file1 file2

# 4  
Old 07-15-2013
Try this if all columns needs to be compared:
Code:
awk 'NR==FNR{A[$1];next}$1 in A || $2 in A || $3 in A' file1 file2

If just column 2 needs to be compared:
Code:
awk 'NR==FNR{A[$1];next}$2 in A' file1 file2

This User Gave Thanks to Yoda For This Post:
# 5  
Old 07-15-2013
It worked, thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. Shell Programming and Scripting

Separate columns into different text files

Hi I have large text file consisting of five columns. Sample of the file is give below: ed 2-4 12.0 commons that they depended on. मानवों नष्ट किया जिन पर वो आधारित थे। ed 3-1 12.0 Almost E, but would be over. रचना करीब करीब ई तक जाती है, मगर तब तो नाटक ख़त्म हो... (2 Replies)
Discussion started by: my_Perl
2 Replies

3. Shell Programming and Scripting

How to parse parts of 1 column into two separate columns?

I have a shell script that is currently transferring a csv file from a server into a Teradata database table. One of the 30 or so columns is called "destination_url". In that URL there are parameters, and it is possible for those parameters to be repeated because of referring companies copying... (3 Replies)
Discussion started by: craigwg
3 Replies

4. UNIX for Dummies Questions & Answers

Concatenate two columns and separate by - (minus)

hi all could you please help me to concatenate two colomns and separate them by "-" the two colomns to concatenate are colomuns 1 and 3 of a very bif file clomn 1 is chr, 2 is snp and 3 is bp the new colomn is chr_B input file : 1 rs1111 10583 1 rs1891 10611 1 rs1807 ... (13 Replies)
Discussion started by: biopsy
13 Replies

5. Shell Programming and Scripting

Separate into columns

Hi all I have following kind of data in a file but non separated ESR1 PA156 leflunomide PA450192 CHST3 PA26503 docetaxel tungstate Pa4586; thalidomide Pa34958; I want to separate entries into columns so that I can put into excel sheet in proper arrangement. I want entries... (4 Replies)
Discussion started by: manigrover
4 Replies

6. Shell Programming and Scripting

Remove brackets repeats and separate in columns

Hi all, I want to remove the remove bracket sign ( ) and put in the separate column I also want to remove the repeated entry like in first row in below input (PA156) is repeated ESR1 (PA156) leflunomide (PA450192) (PA156) leflunomide (PA450192) CHST3 (PA26503) docetaxel... (4 Replies)
Discussion started by: manigrover
4 Replies

7. Shell Programming and Scripting

Comparing columns in two separate files

Hey all, I have a file structure that looks something like this: file1 306708278 88954535 234167885 file2 2012-03-27T12:32:56+00:00 137 Orchotorena 184616310003601409 306708278 es 40.4777947 Majadahonda -3.6416896333333333 0 false atlante83 "<a href=""http://tapbots.com/tweetbot""... (8 Replies)
Discussion started by: dgaff
8 Replies

8. Shell Programming and Scripting

Extracting columns from a matrix and storing each column in a separate file

Hi All, I have a huge matrix file consisting some some millions rows and 6000 columns. The contents are just floating point numbers in the matrix. I want to extract each column (i.e. 6000 of them) and store each column in a separate file. For example, 1.dat will consist of elements from column... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

9. Shell Programming and Scripting

intersect data and name them accordingly - Awk

input1 a1 1 10 1E_gx007 a1 20 30 2E_gx007 a1 50 60 3E_gx007 input2 a1 8 10 J1 100 gx007 a1 20 22 J1 120 gx007 a1 6 10 J2 200 gx007 a1 52 54 J2 220 gx007 a1 28 30 J3 300 gx007 a1 50 54 J3 320 gx007 output (0 Replies)
Discussion started by: repinementer
0 Replies

10. Shell Programming and Scripting

displaying 3 directory listings in 3 separate columns.

i having problems figuring out how to 'read' in 3 different directory listings and then display them on the screen into 3 separate columns. i thought i could use a 'for' loop to grab each directory and then assign a unique variable to each line 'read' in. of course, as you experts all know,... (16 Replies)
Discussion started by: mjays
16 Replies
Login or Register to Ask a Question