Find matching lines between 2 files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Find matching lines between 2 files
# 1  
Old 01-18-2007
Find matching lines between 2 files

How do I find matching lines between two files?
# 2  
Old 01-18-2007
Code:
grep -f File1 File2

# 3  
Old 01-18-2007
Quote:
Originally Posted by anbu23
Code:
grep -f File1 File2


this grep -f will kill time like anything....


consider this custom perl tool,
Code:
#! /opt/third-party/bin/perl
                                                                                                                                              
$i = 1;
open(fh, "small") || die "unable to open the file <small>";
while( chomp($content = <fh>) )
{
  $fileHash{$content} = $i;
  $i += 1;
}
close(fh);
                                                                        
open(file, "big") || die "Unable to open the file <big>";
while ( chomp($rec = <file>) ) {
  print "\nMatch:$rec" if exists $fileHash{$rec} ;
}
close(file);
                                                                        
exit 0

Change the filenames as required, I had posted as it is Smilie Smilie
# 4  
Old 01-18-2007
comm is meant to find lines matching/not matching between files.
if the files are sorted:
Code:
comm -12 file file2

if the files cannot be sorted:
Code:
sort -o tmpfile2 file2
sort -o tmpfile1 file1
comm -12 tmpfile1 tmpfile2

# 5  
Old 01-18-2007
What & why is the -12 used?
# 6  
Old 01-18-2007
I got the comm to work. I used the man comm to find out -12. Thanks a bunch!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two files and count number of matching lines

Dear All, I would like to compare two files and return the number of matches found. Example File A Lx2 L1_Mus1 L1Md_T Lx5 L1M2 L1_Mus3 Lx3_Mus Lx9 Lx2A L1Md_A L1Md_F2 File B L1_Mus3 L1_Mus3 (3 Replies)
Discussion started by: paolo.kunder
3 Replies

2. UNIX for Dummies Questions & Answers

find Search - Find files not matching a pattern

Hello all, this is my first and probably not my last question around here. I do hope you can help or at least point me in the right direction. My question is as follows, I need to find files and possible folders which are not owner = AAA group = BBB with a said location and all sub folders ... (7 Replies)
Discussion started by: kilobyter
7 Replies

3. UNIX for Dummies Questions & Answers

FIND matching pattern of lines in a file

I need to search for two patterns in a file and find number of matching lines. find . -type f | xargs grep "DROP TABLE" | wc -l find . -type f | xargs grep "DROP SYNONYM" | wc -l The above code works. However I am looking at finding a commnd that will simplify as on a singe command... (2 Replies)
Discussion started by: Siva SQL
2 Replies

4. Shell Programming and Scripting

Concatenating 2 lines from 2 files having matching strings

Hello All Unix Users, I am still new to Unix, however I am eager to learn it.. I have 2 files, some lines have some matching substrings, I would like to concatenate these lines into one lines, leaving other untouched. Here below is an example for that.. File 1 (fasta file): >292183... (6 Replies)
Discussion started by: Mohamed EL Hadi
6 Replies

5. UNIX for Dummies Questions & Answers

Grep to find lines matching given patern in a file

When I try with patern matching in a file with below code works cat scj_drive_commands | egrep '/app/oracle/build_lib/pkg32|/app/oracle/build_lib/pkg33' But when I have code with patern searching using of below does not work ! cat scj_drive_commands | egrep... (3 Replies)
Discussion started by: Siva SQL
3 Replies

6. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

7. Shell Programming and Scripting

Look up between 2 files and print matching lines

Hi, I have 2 large log files in .gz format file 1 contains abcde 12345 23456 . . . . . . . . 09123 (8 Replies)
Discussion started by: aravindj80
8 Replies

8. Shell Programming and Scripting

Find the position of lines matching string

I have a file with the below format, GS*8***** ST*1******** A* B* E* RMR*123455(This is the unique number to locate this row) F* SE*1*** GE** GS*9***** ST*2 H* J* RMR*567889(This is the unique number to locate this row) L* SE* GE***** (16 Replies)
Discussion started by: Muthuraj K
16 Replies

9. Shell Programming and Scripting

Perl XML, find matching condition and grep lines and put the lines somewhere else

Hi, my xml files looks something like this <Instance Name="New York"> <Description></Description> <Instance Name="A"> <Description></Description> <PropertyValue Key="false" Name="Building A" /> </Instance> <Instance Name="B"> ... (4 Replies)
Discussion started by: tententen
4 Replies

10. Shell Programming and Scripting

compare two files and to remove the matching lines on both the files

I have two files and need to compare the two files and to remove the matching lines from both the files (4 Replies)
Discussion started by: shellscripter
4 Replies
Login or Register to Ask a Question