The UNIX and Linux Forums  


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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove spaces from first field, and write entire contents into other text file carriehoff Shell Programming and Scripting 3 11-11-2008 02:45 PM
Compare & replace contents within a file kaustubh137 Shell Programming and Scripting 2 05-27-2008 07:56 AM
compare array contents with file orahi001 UNIX for Dummies Questions & Answers 0 03-25-2008 05:44 PM
Query:just need to remove the contents of the file without deleting that namishtiwari Shell Programming and Scripting 3 02-04-2008 05:41 AM
remove the contents of a file surjyap SUN Solaris 2 03-02-2006 04:02 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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..
  #2 (permalink)  
Old 11-17-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Perhpas the diff command will work for you


Code:
> cat file70
abc
def
ghi
jkl
mno
pqr
stu
vwx
yz
123
456
789
0

> cat file71
abc
def
ghi
jkl
mno
pqr
stu
vwx
yz
bash ksh
123
456
789
0
unix.com

> diff file70 file71 | grep "^>" | cut -c3-
bash ksh
unix.com

  #3 (permalink)  
Old 11-17-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

to print the different lines of two files try:


Code:
comm -3 file1 file2

and for further informations


Code:
man comm

Kind regards

Chris
  #4 (permalink)  
Old 11-18-2008
royalibrahim royalibrahim is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 86
Thanks to you all for the suggestions. But anyone has any awk, perl code to do this task?

And Also, the below perl code will remove duplicate, non-consecutive lines based on the last field without sorting. Now, please tell me, what should I change in this code in order to print unique lines of a file by just not seeing the last field but the entire line (the whole record)?

Code:
perl -ane'print unless $_{$F[-1]}++'

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:16 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0