The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 11-17-2008
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
Compare two files and remove all the contents of one file from another

Hi,

I have two files, in which the second file has exactly the same contents of the first file with some additional records. Now, if I want to remove those matching lines from file2 and print only the extra contents which the first file does not have, I could use the below unsophisticated command, consider f1 and f2 are the two files
Code:
var=`cat f1`
grep -v "$var" f2
but I need a more optimal solution with fast and reliable with less memory consumption.

I have found these 2 lines of code, but it does not work for files having lengthier lines:

Code:
fgrep -v -x -f f2 f1  
awk 'NR==FNR {b[$0]; next} !($0 in b)' f2 f1

Last edited by royalibrahim; 11-18-2008 at 12:51 AM..