how to extract fields for /etc/passwd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to extract fields for /etc/passwd
# 1  
Old 03-30-2006
how to extract fields for /etc/passwd

hi,

i would like to extract the just name field from /etc/passwd file, and nothing else. ie grep pauline, and Pauline Fowler comes back.


cheeers
paul mc
# 2  
Old 03-30-2006
Code:
PASSWORD_FILE=/etc/passwd
n=1
for name in $(awk 'BEGIN{FS=":"}{print $1}' < "$PASSWORD_FILE" )
do
   echo "USER #$n = $name"
   (( n += 1 ))
done

Regards
# 3  
Old 03-30-2006
hi there are easier ways..

just found this

cat /etc/passwd | awk -F ":" '{print $6}' #this displays 6th field ie home directories

awk -F\: '{print $6}' /etc/passwd |grep pauline #this displays pauline home directory

or echo ~pauline.fowler
# 4  
Old 03-30-2006
u can use the cut to extract the field required..this case the fifth field(try to check to make sure that urs system is using the same format for passwd file)and then u can use the grep command to find the user name
cut -d':' -f5 /etc/passwd |grep nameulookingfor
or u can use grep nameulookingfor /etc/passwd | cut -d':' -f5 ...


may be awk users can provide with more options...using the print{$5} options....
# 5  
Old 03-30-2006
thanks
klashxx that was really useful
# 6  
Old 03-30-2006
Hi,

When i run the following shell its giving an errorin line 5 ? syntax error $' unexpected

What is the error???

#!/bin/sh
log=/u01//userlist.log
PASSWORD_FILE=/etc/passwd
n=1
for name in $(awk 'BEGIN{FS=":"}{print $1}' < "$PASSWORD_FILE" )
do
echo "USER #$n = $name" > $log
(( n += 1 ))
done


Many Thanks,
# 7  
Old 03-31-2006
change #!/bin/sh

to

#!/bin/ksh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. UNIX for Dummies Questions & Answers

Extract user accounts and home directory from /etc/passwd.

I am trying to obtain all user accounts and their respective home directories. /etc/passwd contains the required information, but I want to filter it to only show the uid,username and home directory path. I am working on a Solaris 11 machine. I made a little headway so far, but I got stuck... (7 Replies)
Discussion started by: Hijanoqu
7 Replies

3. UNIX for Dummies Questions & Answers

passwd -S on linux- what are the fields?

I'm looking for some documentation on what the different fields mean in the output of passwd -S username: passwd -S foo foo PS 2012-03-20 0 70 3 -1 (Password set, MD5 crypt.) I think the date given is the date of the last password change, the 0 after that is the minimum password age, and... (2 Replies)
Discussion started by: Anne Neville
2 Replies

4. Shell Programming and Scripting

Extract fields from different rows.

Hi, I have data like below. SID=D6EB96CC0 HID=9C246D6 CSource=xya Cappe=1 Versionc=3670 MAR1=STL MARS2=STL REQ_BUFFER_ENCODING=UTF-8 REQ_BUFFER_ORIG_ENCODING=UTF-8 RESP_BODY_ENCODING=UTF-8 CON_ID=2713 I want to select CSource=xya (18 Replies)
Discussion started by: chetan.c
18 Replies

5. UNIX for Dummies Questions & Answers

How to extract fields from etc/passwd file?

Hi! i want to extract from /etc/passwd file,the user and user info fileds, to a another file.I've tried this: cut -d ':' -f1 ':' -f6 < file but cut can be used to extract olny one field and not two. maybe with awk is this possible? (4 Replies)
Discussion started by: strawhatluffy
4 Replies

6. Shell Programming and Scripting

Cut fields from /etc/passwd file into variables?

The script must ask the user to enter the user name and check whether the user exists in /etc/passwd (you must allow the partial usernames also). If the username exists, display the details as: List of users Login Name: User ID: ... (3 Replies)
Discussion started by: nobletechnology
3 Replies

7. Shell Programming and Scripting

Extract fields

Hi, I have a tmp file like below: <ADATA> ANUM=900 ADESC=Saving ATYP=0 TXREGD=0 </ADATA> <ADATA> ANUM=890 ADESC=Saving ATYP=0 ABAL=9000 TXREGD=1 </ADATA> <ADATA> (2 Replies)
Discussion started by: kunigirib
2 Replies

8. Shell Programming and Scripting

Script to extract fields

Dear all, please see the logs shown below: 12-12 08:47:37.545 DBG AGIML SERVER..............................write() (<agiml><header><responsetype>TOPUP</responsetype></header><response><auditfields/><resultcode>999</resultcode>... (7 Replies)
Discussion started by: akhtar.bhat
7 Replies

9. Shell Programming and Scripting

Extract fields from from this

thank youuuu (0 Replies)
Discussion started by: rnallamothu
0 Replies

10. UNIX for Dummies Questions & Answers

Trying to extract a field from /etc/passwd file..

Hello, was looking for some help on extracting a field from the passwd file. So far I have made a copy of the passwd file and changed my rights so I can edit it. Every user's password is coded as an :x:, and my goal was to change that x to a blank, and then try to extract any user with that field... (2 Replies)
Discussion started by: xBuRnTx
2 Replies
Login or Register to Ask a Question