common entries of first column in 2 or 3 files:kindly check


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting common entries of first column in 2 or 3 files:kindly check
# 1  
Old 08-01-2012
common entries of first column in 2 or 3 files:kindly check

Hi all,

I have 3 files with such data

first files

Quote:
xyz 123
abc 345
ghx 123
second file


Quote:
sef 345
xyz 456
ghx 347
third file

Quote:
rex 789
xyz 908
rty 567
I have to find common entries of first column in two ways

1) between 2 files
2)between 3 files

for eg out put for above input in first and second file shuld be

Quote:
xyz
ghx
and all the three files will be
Quote:
xyz
Thanks
Mani
# 2  
Old 08-02-2012
try
Code:
awk 'BEGIN{while(getline < "file1"){a[$1]++}}NR==FNR{if(a[$1]){print $1 > "file_12";b[$1]++}}NR!=FNR{if(b[$1]){print $1 > "file_123"}}' file2 file3

look for output in file_12 and file_123
# 3  
Old 08-02-2012
awk

Hi,

Try this one,
Code:
awk 'FNR==NR{a[$1]=1;next;}{if(a[$1]){print $1;}}' file1 file2 >comm12

awk 'FNR==NR{a[$0]=1;next;}{if(a[$1]){print $1;}}' comm12 file3 >comm123

here i am assuming that you delimiter is <space> else specify that.
Cheers,
Ranga:-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to check 2 log files for a common pattern?

hi! im new here and to unix. I want to do something with our log files. to compare two log files for a certain pattern. sample: file1.log contains all the "successful" run of a procedure. file2.log contains all the "current" running procedures. sample line from file1.log... (5 Replies)
Discussion started by: cabs_14
5 Replies

2. UNIX for Dummies Questions & Answers

How to join 2 .txt files based on a common column?

Hi all, I'm trying to join two .txt file tab delimitated based on a common column. File 1 transcript_id gene_id length effective_length expected_count TPM FPKM IsoPct comp1000201_c0_seq1 comp1000201_c0 337 183.51 0.00 0.00 0.00 0.00 comp1000297_c0_seq1 ... (1 Reply)
Discussion started by: alisrpp
1 Replies

3. Shell Programming and Scripting

Merging 2 text files when there is a common time stamp column in them

Dear Unix experts and users I have 2 kinds of files like below, of which I need to merge them in the order of time. File1: Date_Time Context D1 D2 04/19/2013_23:48:54.819 ABCD x x 04/19/2013_23:48:55.307 ABCD x x 04/19/2013_23:48:55.823 ABCD x ... (7 Replies)
Discussion started by: ks_reddy
7 Replies

4. Shell Programming and Scripting

common entries between files based on 1st column

Hi, I am trying to get the common entries from 2 files based on 1st field.. However when I try to do in perl I am getting blank output.. How can I do this in awk? open(BUFF1, "my_genes"); open(BUFF3, "rawcounts"); #open(WRBUFF,">result_rawcounts"); while($line =<BUFF1>) { ... (3 Replies)
Discussion started by: Diya123
3 Replies

5. Shell Programming and Scripting

Kindly check it: Camparison of files only column1 of 2 files

Hi all, I have 2 files in which i have to find commom entries in column 1 an dif soemthing is common write other data of both files in front of it mentioned. Gene symbol and disease name column 1 column2 ARFGEF2 CAD DDEF2 CAD PSCD3 CAD PSCD4 CAD CAMK1... (15 Replies)
Discussion started by: manigrover
15 Replies

6. Shell Programming and Scripting

Request to check:find out common entries

I have to compare 2 files which means 2 files with common entries in same column and separate those common entries in a diferent file as well right before those entries common so that I can separat common and Uncommon entries in rows in 2 different files. Is it possible For eg. one file ... (3 Replies)
Discussion started by: manigrover
3 Replies

7. UNIX for Dummies Questions & Answers

Writing a loop to merge multiple files by common column

I have 100 data files labelled 250.1.txt through 250.100.txt. The second column of the data files partially match (there is about %90 overlap). Each data file has 4 columns. I want the merge all these text files by the matching values in the second column. In the output, the first column should... (1 Reply)
Discussion started by: evelibertine
1 Replies

8. Shell Programming and Scripting

Join multiple files based on 1 common column

I have n files (for ex:64 files) with one similar column. Is it possible to combine them all based on that column ? file1 ax100 20 30 40 ax200 22 33 44 file2 ax100 10 20 40 ax200 12 13 44 file2 ax100 0 0 4 ax200 2 3 4 (9 Replies)
Discussion started by: quincyjones
9 Replies

9. Shell Programming and Scripting

Merging 2 files based on a common column

Hi All, I do have 2 files file 1 has 4 tab delimited columns 234 a c dfgyu 294 b g fih 302 c h jzh 328 z c san 597 f g son File 2 has 2 tab delimted columns 234 23 302 24 597 24 I want to merge file 2 with file 1 based on the data common in both files which is the first column so... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

10. Shell Programming and Scripting

Merging two files with a common column

Hi, I have two files file1 and file2. I have to merge the columns of those two files into file3 based on common column of two files. To be simple. file1: Row-id name1 13456 Rahul 16789 Vishal 18901 Karan file2 : Row-id place 18901 Mumbai ... (2 Replies)
Discussion started by: manneni prakash
2 Replies
Login or Register to Ask a Question