User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,429 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,637 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 2311 | Replies: 2
Reply
Join Date: May 2005
Posts: 229
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Comparing two files...

  #1  
Aug 7th, 2007
Hey, 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:

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, I want to print it out, 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:

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2005
Posts: 229
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Comparing two files...

  #2  
Aug 7th, 2007
How about something like this:

Do the awk to get all the usernames out:
root# awk -F: '{ print $1 }' /etc/passwd > users.txt
Then do a for loop with a grep -v against all the usernames in /etc/passwd? Like this:

for x in `cat users.txt` ; do
> grep -v $x myusers
> done

Only problem with that is, it doesn't compile one nice list. It does it for every line in the users.txt file, thus printing out every line of the myusers file for every line in the /etc/password file. That's a mess. Any ideas with that?

Or what about sed? Couldn't I do a similar for loop with sed, and remove every line that matches $x? I have tried this to, but it's not working. Any ideas would help.
Reply With Quote  
Join Date: May 2005
Posts: 229
Reputation: nathanpacker is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: Comparing two files...

  #3  
Aug 7th, 2007
found something that works:

awk -F: 'FNR==NR { 
              users[$2]
              next
         } 
         !( $1 in users ) { print $0 }' myusers /etc/passwd
Last edited by nathanpacker : Aug 7th, 2007 at 7:27 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:24 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC