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
awk - comparing files dbrundrett Shell Programming and Scripting 4 08-22-2008 07:23 AM
Comparing two files ragavhere Shell Programming and Scripting 31 06-12-2008 01:12 AM
Comparing two files superstar003 Forum Support Area for Unregistered Users & Account Problems 1 05-08-2008 12:34 AM
Comparing two files.. padarthy Shell Programming and Scripting 1 08-29-2007 05:01 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-08-2007 10:38 PM

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

Join Date: Jul 2007
Posts: 20
Stumble this Post!
Comparing two files...

I searched through the forums, and there are a couple threads that have a similar problem to mine, but they don't seem to exactly address my problem.

I'm running an HP-UX box, trying to create a little script that will compare the /etc/passwd file with another file I have created.

Each line in this file, let's call it /etc/myusers, has a pin that the user has chosen (not necessarily unique), then their userid, then the comments field from /etc/passwd. (basically their full name, phone number, all that jazz.

Here is an example:


Quote:
pin:userid:comments:
1234:pittb:"Brad Pitt,Los Angeles CA,123-123-1234":
9876:clooneyg:"George Clooney,Los Angeles CA,123-123-1234":
6548:dansont:"Ted Danson,Los Angeles CA,123-123-1234":
9685:owenc:"Clive Owen,Los Angeles CA,123-123-1234":
1652:simpsonh:"Homer Simpson,Los Angeles CA,123-123-1234":

I basically want to make sure that every account in my /etc/passwd file is also in this file. So all I want to do is go through each line in /etc/passwd, and search for each userid in this custom file. If the name DOESN'T exist in my custom file, I want to print it out (from /etc/passwd) to the screen, or a file, or whatever, so I can go through and add all the accounts to this file.

It should be fairly simple, I just can't quite figure it out.

I was thinking of just doing an awk on the /etc/passwd file and pulling out all the usernames to a file, and then doing a for loop through that file, or something like this:


Code:
root# awk -F: '{ print $1 }' /etc/passwd > users.txt

root# for x in `cat users.txt` ; do
>grep $x /etc/myusers
>if no result, print to file or whatever...?
Just not quite sure what.

Thanks for your help.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 08-07-2007
Shell_Life's Avatar
Unix/Informix/4GL/SQL
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Stumble this Post!
Code:
cut -d':' -f2 /etc/myusers > $$TempUsers
cut -d':' -f1 /etc/passwd > $$EtcUsers
diff $$TempUsers $$EtcUsers
rm -f $$*
Reply With Quote
  #3 (permalink)  
Old 08-07-2007
Registered User
 

Join Date: Jul 2007
Posts: 20
Stumble this Post!
Quote:
Originally Posted by Shell_Life View Post
Code:
cut -d':' -f2 /etc/myusers > $$TempUsers
cut -d':' -f1 /etc/passwd > $$EtcUsers
diff $$TempUsers $$EtcUsers
rm -f $$*
Well, I'm not sure how to read that ouput from the diff command. I looked at the man page, but it still doesn't make much sense. I can see that it's listing all the users, and then every so often printing some weird numbers, I'm assuming are correlating to the lines or something.

What I need is a nice clean output of simply the lines in /etc/password that do not have a corresponding userid in /etc/myusers.
Reply With Quote
  #4 (permalink)  
Old 08-07-2007
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,546
Stumble this Post!
Code:
awk -F":" 'BEGIN{ while(getline < "passwordfile") { arr[$2]=i++ } }{ for ( i in arr ) { if ( i == $2 ) { cnt=1} } if ( cnt != 1 ) { print } cnt=0; }' userfile
Reply With Quote
  #5 (permalink)  
Old 08-07-2007
Registered User
 

Join Date: Jul 2007
Posts: 20
Stumble this Post!
I apologize for being such a noob at all this. But all that command seems to do is print out the contents of the users file referenced at the end.
Reply With Quote
  #6 (permalink)  
Old 08-07-2007
Registered User
 

Join Date: Aug 2007
Location: New Delhi India/Denver USA
Posts: 4
Stumble this Post!
one good logic is to ...cut and have column 2nd then 1st on each line from passwd file and redirect the output to file lets say a1. Then do the same on other passwd file and save the file as a2. Sort both file one by one and redirect output to a1sorted and a2sorted. Now run "sdiff a1sorted a2sorted"
and it will show you where we have extra members or less members..


arun

Quote:
Originally Posted by paqman View Post
I searched through the forums, and there are a couple threads that have a similar problem to mine, but they don't seem to exactly address my problem.

I'm running an HP-UX box, trying to create a little script that will compare the /etc/passwd file with another file I have created.

Each line in this file, let's call it /etc/myusers, has a pin that the user has chosen (not necessarily unique), then their userid, then the comments field from /etc/passwd. (basically their full name, phone number, all that jazz.

Here is an example:





I basically want to make sure that every account in my /etc/passwd file is also in this file. So all I want to do is go through each line in /etc/passwd, and search for each userid in this custom file. If the name DOESN'T exist in my custom file, I want to print it out (from /etc/passwd) to the screen, or a file, or whatever, so I can go through and add all the accounts to this file.

It should be fairly simple, I just can't quite figure it out.

I was thinking of just doing an awk on the /etc/passwd file and pulling out all the usernames to a file, and then doing a for loop through that file, or something like this:


Code:
root# awk -F: '{ print $1 }' /etc/passwd > users.txt

root# for x in `cat users.txt` ; do
>grep $x /etc/myusers
>if no result, print to file or whatever...?
Just not quite sure what.

Thanks for your help.
Reply With Quote
  #7 (permalink)  
Old 08-07-2007
reborg's Avatar
Administrator
 
Join Date: Mar 2005
Location: Ireland
Posts: 3,513
Stumble this Post!
Code:
awk -F: 'FNR==NR { 
              users[$2] 
         } 
         FNR != NR { 
              if ( $1 in users ) { 
                   next 
              } else { 
                   print $1 
              }
         }' <userfile> /etc/passwd
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
awk, perl script

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:05 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