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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Deleting the files comparing the creation dates pssandeep UNIX for Dummies Questions & Answers 2 04-10-2008 07:54 AM
How to create a list of entries without vi mpc8250 Shell Programming and Scripting 3 02-12-2008 11:56 PM
Searching list of entries in file for actual files in dir not4google UNIX for Dummies Questions & Answers 2 10-18-2006 08:24 AM
Comparing a distinct value in 1 list with another list manualvin Shell Programming and Scripting 6 06-22-2004 03:42 AM
deleting double entries in a log file opusforum Shell Programming and Scripting 5 09-17-2002 12:02 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-14-2007
Registered User
 

Join Date: Jul 2007
Posts: 54
Post Comparing 2 list and deleting deuplicate entries

Hi,

I have a list of names and I want to basically take each name and search a given file to see if the name is in that file. If it is then delete the name from the list and if it isn't then leave it. Anyone know how to script something like this. Here's an example of what I mean:

List 1:
Tom
Bob
John
Dave

List 2:
Anthony
Ed
Bob
Dave

Comparing List 1 to List 2 results in List1 being:
Tom
John

And just to note, List 2 might not be a list, could just be a text document with the names in there but the script should work about the same way. Can anyone help with this? Thanks!

Last edited by eltinator; 08-14-2007 at 01:37 PM. Reason: Correction
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-14-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,418
Your example seems to be backwards from your worded request. List1 should be Tom John?

Anyway, one way to do what you seem to ask
Code:
grep -v -f List2 List1> newlist1; mv newlist1 List1
Reply With Quote
  #3 (permalink)  
Old 08-14-2007
Registered User
 

Join Date: Jul 2007
Posts: 54
shoot sorry, my example didnt make sense. It is backwards ^^*

Thanks!
Reply With Quote
  #4 (permalink)  
Old 08-14-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,442
here is with awk considering backwards option
Code:
awk 'BEGIN{ while ( getline < "2" ) { arr[$0] = i++; } } { if (!( $0 in arr )) { print } }' 1
Reply With Quote
  #5 (permalink)  
Old 08-14-2007
Registered User
 

Join Date: Jul 2007
Posts: 54
Quote:
Originally Posted by jim mcnamara View Post
Your example seems to be backwards from your worded request. List1 should be Tom John?

Anyway, one way to do what you seem to ask
Code:
grep -v -f List2 List1> newlist1; mv newlist1 List1
Quick question on this. It works nicely when there are just 2 list but what when I try it for a file that has some sentences it doesn't seem to work. For example, List1 is as above but List2 is something like:


List of names

Names:
Anthony
Ed
Bob
Dave

After running the command List1 is empty, like it couldn't match anything from List2 due to the added lines in the beginning. Any ideas? Thanks!

Last edited by eltinator; 08-14-2007 at 01:19 PM. Reason: Mistake
Reply With Quote
  #6 (permalink)  
Old 08-14-2007
Registered User
 

Join Date: Jul 2007
Posts: 54
Ok further testing shows that the grep has problems when List2 has a line with nothing on it. Basically it works when there are no blank lines so doing something like:

Dave

Bob
Jim
Tim

doesn't work. Is there any way to fix grep for this?
Reply With Quote
  #7 (permalink)  
Old 08-14-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,442
Did you get a chance to try out what I had posted ?

Any problems with that !

Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:32 AM.


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

Content Relevant URLs by vBSEO 3.2.0