•
•
•
•
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
![]() |
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:
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:
Just not quite sure what.
Thanks for your help.
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.
How about something like this:
Do the awk to get all the usernames out:
Then do a for loop with a grep -v against all the usernames in /etc/passwd? Like this:
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.
Do the awk to get all the usernames out:
root# awk -F: '{ print $1 }' /etc/passwd > users.txtfor 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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- hjsplit doenst work for avi files? (Windows NT / 2000 / XP / 2003)
- Java code to compare XML files (Java)
- Is this software any good? (Viruses, Spyware and other Nasties)
- Trojan virus - need help please. (Viruses, Spyware and other Nasties)
- Does Samba send deleted files to a recycle bin? (*nix Software)
- Cannot transfer files from one hardrive to another =[ please help (Windows NT / 2000 / XP / 2003)
- FTP files (Geeks' Lounge)
Other Threads in the Shell Scripting Forum
- Previous Thread: Need help in sorting
- Next Thread: Sendmail Log Watch


Linear Mode