LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 01-27-2009, 06:35 AM   #1
xpucto
Member
 
Registered: Sep 2005
Location: Madrid, Spain
Distribution: Suse
Posts: 492

Rep: Reputation: 30
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,967

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
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
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 458

Rep: Reputation: 58
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,967

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
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: Madrid, Spain
Distribution: Suse
Posts: 492

Original Poster
Rep: Reputation: 30
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: Madrid, Spain
Distribution: Suse
Posts: 492

Original Poster
Rep: Reputation: 30
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
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: OpenSUSE 12.1 CentOS 6.2
Posts: 8,967

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I compare floating number in a shell script? abefroman Programming 27 11-28-2011 07:23 PM
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


All times are GMT -5. The time now is 10:55 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration