The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pattern matching in file and then display 10 lines above every time namishtiwari Shell Programming and Scripting 16 10-20-2008 07:50 PM
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 03:37 AM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 01:15 AM
Returning filename and matching lines smb_uk Shell Programming and Scripting 2 10-05-2007 03:08 PM
grep something to find which files, lines contain something yls177 UNIX for Dummies Questions & Answers 4 11-26-2002 09:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-18-2007
Registered User
 

Join Date: Jul 2006
Posts: 11
Find matching lines between 2 files

How do I find matching lines between two files?
Reply With Quote
Forum Sponsor
  #2  
Old 01-18-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
grep -f File1 File2
Reply With Quote
  #3  
Old 01-18-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
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
Reply With Quote
  #4  
Old 01-18-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
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
Reply With Quote
  #5  
Old 01-18-2007
Registered User
 

Join Date: Jul 2006
Posts: 11
What & why is the -12 used?
Reply With Quote
  #6  
Old 01-18-2007
Registered User
 

Join Date: Jul 2006
Posts: 11
I got the comm to work. I used the man comm to find out -12. Thanks a bunch!
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:20 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0