1833772 Members
1988 Online
110063 Solutions
New Discussion

Grep command

 
SOLVED
Go to solution
Karthik S S
Honored Contributor

Grep command

Hi,

We are planning to move some 50 users from one system to the other. I have a list user IDs for those 50 users. I am planning to create user accounts for them on the new machine by taking the /etc/passwd entries from the old machine. Now how do I grep the /etc/passwd file so that the user id is matched only with the first word of the passwd file (ie. the user ID field). Because when I do the follwoing,

for i in `cat 50users`
do
grep -w $i /etc/passwd >> /tmp/50passwdentries
done

The user id which is present in the comment field of passwd file is also taken in to consideration so that I am ending up with having duplicate entries. Pl. help.

Thanks in advance
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
5 REPLIES 5
Stefan Farrelly
Honored Contributor

Re: Grep command


grep -w ^$i /etc/passwd

will search at the start of the password file only. You could further delimiter it with;

grep -w ^$i: /etc/passwd

so that it only finds the exact userid up until the first :

Im from Palmerston North, New Zealand, but somehow ended up in London...
Karthik S S
Honored Contributor

Re: Grep command

Hi,

with grep ^$i: it is able to get only 5 or 6 users. What could be the problem??

Thanks
Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Carlos Fernandez Riera
Honored Contributor
Solution

Re: Grep command

Writing your file as:
^user1:
^user2:
....


you can use the -f flag for grep:


grep -w -f 50users_file /etc/passwd.
unsupported
Stefan Farrelly
Honored Contributor

Re: Grep command

Ive tested it with my password file, a list of all userids and our for loop command and it worked fine - it matches all exactly only once each. I didnt use the -w though - in fact its an illegal option no HP-UX. Hmm.

Did you remove your /tmp/50passwdentries file before you run your for loop each time ?

You will have to provide a list of your 50users and the passwd file in order to diagnose it further.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Karthik S S
Honored Contributor

Re: Grep command

Hi

Thank you carlos.
grep -w -f solved my problem.

Stefan: grep -w works with 11i :-). Thanks for your help too.

Karthik
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn