Operating System - HP-UX
1835245 Members
2537 Online
110078 Solutions
New Discussion

awk user name and name from passwd

 
SOLVED
Go to solution
MarkSyder
Honored Contributor

awk user name and name from passwd

Hi everybody,

I'm trying to produce a list of users from the passwd file. The only info I want to extract is user id and name (fields 1 and 5). I've tried the following:

for i in `cat passwd|awk -F: '{print $1, $5}'`
do
echo $i >> newfile
done

but the user id and name always end up on different lines. I've tried putting \c and "\c" inbetween the $1 and $5 but it has not had any effect.

Any ideas?

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
9 REPLIES 9
Karthik S S
Honored Contributor
Solution

Re: awk user name and name from passwd

cat /etc/passwd | awk -F ":" '{print $1,$5}'

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

Re: awk user name and name from passwd

cut -f1,5 -d":" /etc/passwd >> newfile


Will do it

cat /etc/passwd|while read line
do
echo $line|awk -F: '{print $1, $5}' >> newfile
done


Steve STeel
If you want truly to understand something, try to change it. (Kurt Lewin)
MarkSyder
Honored Contributor

Re: awk user name and name from passwd

10 out of 10 Karthik! But 0 out of 10 for the person who wrote the manual I'm working from.

Mark
The triumph of evil requires only that good men do nothing
MarkSyder
Honored Contributor

Re: awk user name and name from passwd

Steve - Karthik's solution worked, so I didn't try yours. Three points to acknowledge the fact that you tried to help.

This thread is now closed as it is working perfectly.

Mark
The triumph of evil requires only that good men do nothing
Robert A. Pierce
Frequent Advisor

Re: awk user name and name from passwd


I know the thread is closed, but just for
reference, you don't need to use `cat`:

awk -F":" '{print $1,$5}' /etc/passwd


Rob Pierce
--
9 months to Christmas!
Paula J Frazer-Campbell
Honored Contributor

Re: awk user name and name from passwd

Rob

By any chance do you still believe in Santa?


Paula
If you can spell SysAdmin then you is one - anon
Bill Hassell
Honored Contributor

Re: awk user name and name from passwd

How about:

logins

The formatting is already done for you and there are a plethora of options including the very useful -d to locate duplicate user ID's.


Bill Hassell, sysadmin
MarkSyder
Honored Contributor

Re: awk user name and name from passwd

The thread may have been closed, but reducing the amount of typing I do merits some thanks - Santa Syder is going to give more points.

Sorry, but asking someone if they still believe in Santa (or Father Christmas as we call him in this country) does not qualify for points!

Bill - logins would have reduced my typing still further, but I tried it twice and it caused a core dump both times.
Mark
The triumph of evil requires only that good men do nothing
Bill Hassell
Honored Contributor

Re: awk user name and name from passwd

Oh, oh. I'd check on patches but also run pwck to see if /etc/passwd is OK. logins has been around for a long time and works on Trusted and unTrusted alike.


Bill Hassell, sysadmin