find common data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find common data
# 1  
Old 03-11-2012
find common data

Hey guys,

I have two files. file1 and file2.

file1:
Code:
a,1
b,2
c,343
d,343
e,4343
f,4544

file 2:
Code:
a,
d,
e,

Now i need to find the common data between these files from file1.
i.e
Code:
a,1
d,343
e,4343

Please help regaridng the same.
Thanks in advance.!!!

Last edited by Franklin52; 03-11-2012 at 07:01 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 03-11-2012
Hi there is the comm command just for this, the files should be sorted. The command is then;

Code:
sort file1 > file01
sort file2 > file02
comm -12 file01 file02

Regards

Dave

Last edited by gull04; 03-11-2012 at 06:07 AM.. Reason: Typo
# 3  
Old 03-11-2012
The comm command will only fetch the common data between them.

but i need the same thing from file1 as file1 has more info about the elements..
# 4  
Old 03-11-2012
Sorry I misread the data, I'll have a little think about this but not so hot with awk which is probably ideal for this.

Regards

Dave
# 5  
Old 03-11-2012
Try:
Code:
awk -F, 'NR==FNR{a[$1];next}$1 in a' file2 file1

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 03-11-2012
In very simpler way if we do :
Code:
cat file2 | while read line
do
  grep $line file1
done


Last edited by Franklin52; 03-11-2012 at 07:13 AM.. Reason: Please use code tags for data and code samples, thank you
# 7  
Old 03-11-2012
Quote:
Originally Posted by msabhi
In very simpler way if we do :
Code:
cat file2 | while read line
do
  grep $line file1
done

Looks simpler but the awk solution is much more efficient.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find common words

Hi, I have 10 files which needs to be print common words from those all files. Is there any command to find out. (2 Replies)
Discussion started by: munna_dude
2 Replies

2. Shell Programming and Scripting

Find the common values

Hi, I have two files with the below values. file1 305231921 1.0 ben/Ben_Determination_Appeals 1348791394 2.0 ben/Ben_Determination_Appeals] 1305231921 1.0 ben/Cancel_Refund_Payment_JLRS 1348791394 2.0 ben/Cancel_Refund_Payment_JLRS 1305231921 ... (2 Replies)
Discussion started by: Vikram_Tanwar12
2 Replies

3. Shell Programming and Scripting

Help on writing data from 2 different files to one based on a common factor

Hello all, I have 2 text files. For example: File1.txt contains data A B C D ****NEXT**** X Y Z ****NEXT**** L M N and File2.txt contains data (13 Replies)
Discussion started by: vat1kor
13 Replies

4. Shell Programming and Scripting

Find Common Values Across Two Files

Hi All, I have two files like below: File1 MYFILE_28012012_1112.txt|4 MYFILE_28012012_1113.txt|51 MYFILE_28012012_1114.txt|57 MYFILE_28012012_1115.txt|57 MYFILE_28012012_1116.txt|57 MYFILE_28012012_1117.txt|57 File2 MYFILE_28012012_1110.txt|57 MYFILE_28012012_1111.txt|57... (2 Replies)
Discussion started by: angshuman
2 Replies

5. UNIX for Dummies Questions & Answers

Extract common data out of multiple files

I am trying to extract common list of Organisms from different files For example I took 3 files and showed expected result. In real I have more than 1000 files. I am aware about the useful use of awk and grep but unaware in depth so need guidance regarding it. I want to use awk/ grep/ cut/... (7 Replies)
Discussion started by: macmath
7 Replies

6. Shell Programming and Scripting

Find common entries

Hi all I have to compare two files and find common entries First file is like this XVY CVY ZYN MNA In second file I have to search these entries in even number of columns 5 XVY 7 hdfj 8 CVY 9 if there is common entries then out put shuld be 5 XVY(approved) 7 hdfj 8... (11 Replies)
Discussion started by: manigrover
11 Replies

7. Shell Programming and Scripting

Help with duplicate common data content

Input file: #data_131 0 >content..._* 1 >content..._at_+/97.20% #data_137 0 >content..._* 1 >content..._at_+/97.20% 2 >seq..._* 3 >content..._at_+/97.20% 4 >content..._at_+/97.20% #data_141 0 >content..._* #data_150 0 >content..._* 1 >content..._at_+/97.20% 2 >seq..._* 3... (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Shell Programming and Scripting

Find common entries in 2 list and write data before it

Hi all, I have 2 files: second file I want if entries in one file will match in other file. It shuld wite approve before it so output shuld be (1 Reply)
Discussion started by: manigrover
1 Replies

9. Ubuntu

How to compare two columns and fetch the common data with additional column

Dear All, I am new to this forum and please ignore my little knowledge :p I have two types of data (a subset is given below) data version 1: 439798 2 1 451209 1 2 508696 2 1 555760 2 1 582757 1 2 582889 1 2 691827... (2 Replies)
Discussion started by: evoll
2 Replies
Login or Register to Ask a Question