LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2009, 06:35 AM   #1
xpucto
Member
 
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Rep: Reputation: 31
shell script tp compare to liste of names


Hi!

I would like to write a script that compare 2 lists of name:1 list contains all names of a DB, 2 second list contains only the names in the DB of activ users. I would like to compare the two lists in oder to get a list of non active users (users that are in the 1st list but not in the second list).
So if the 1st List looks like this:
Quote:
John
Bill
Helen
Chris
Thomas
and the second like with active user this
Quote:
Helen
Chris
Then the script schould create a 3rd list of inactive users:
Quote:
John
bill
Thomas
I would be very thankful if I could get any help with it.

Last edited by xpucto; 01-27-2009 at 06:40 AM.
 
Old 01-27-2009, 06:43 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
This is a task for the comm command, for example
Code:
comm -3 <(sort list1) <(sort list2)
See man comm for details and look at process substitution in the Advanced Bash Scripting Guide about passing the output of a process as input to another process (as I did to sort the two list in the example above).
 
Old 01-27-2009, 06:44 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Using something like awk or perl, you should put the data into "associative arrays" aliass "hashes" with the key value as the name and two flags, one each for occurence in each file.

After scanning both files, the key values flag 2 are the current list, and so the key values with no flag 2 are your exception.

Try it.

End
 
Old 01-27-2009, 07:05 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Just seen the editing of your first post. I can suggest an alternative to my previous post, that will preserve the order of the names in the first list:
Code:
grep -f list2 -v list1
This will read patterns from list2 and will exclude them from list1.
 
Old 01-27-2009, 07:16 AM   #5
xpucto
Member
 
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Original Poster
Rep: Reputation: 31
Thanks a lot for your help! comm -3 has done the job!
 
Old 01-27-2009, 07:17 AM   #6
xpucto
Member
 
Registered: Sep 2005
Location: Vienna, Austria
Distribution: Mint 13
Posts: 524

Original Poster
Rep: Reputation: 31
thanks a lot for your help! comm -3 has done the job.

Quote:
grep -f list2 -v list1
even nicer! thanks!
 
Old 01-27-2009, 08:21 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
A slight correction to myself: the grep command has a side effect, since it consider the names in list2 as patterns and not as real names. That is if you have two similar names like Bill and Billie in list1 and only Bill in list2, both of them in list1 will be excluded. You can avoid this behavior if you use option -w of grep to force the pattern to be considered as a whole word, not just a pattern inside a line:
Code:
grep -w -f list2 -v list1
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 34 10-12-2016 08:09 AM
Shell Script Compare Folders corteplaneta Programming 8 09-18-2010 01:32 AM
How to Compare Two files using shell script pooppp Linux - Networking 14 08-05-2008 03:35 AM
shell script: compare 2 files anhtt Programming 6 08-29-2007 02:39 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration