Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For a homework assignment, I need to use grep to parse /etc/passwd and get a list of all users (their usernames only, nothing else). It should use a pipe, and the results should end up in the allusers file. This is what I have so far:

grep -i etc/password | .... >allusers 

What am I missing?

share|improve this question
    
That grep looks for patterns and normally prints out the whole line, you need to limit its output to just the match. Check the manpage. –  vonbrand Feb 19 '13 at 3:54

4 Answers 4

Hint

Each field in a /etc/passwd line is separated by a colon, and the username is the first field, so you need to filter each line to only show the characters up to the first colon

Answer

grep is not even close to the best tool for doing this, but if you're required to use it, this will work:

grep -oE '^[^:]+' /etc/passwd

-o tells it to only return the part of the line that matches. -E turns on extended regular expressions so the + will work later. ^ matches the beginning of the line, [^:] matches anything except a colon, and + means as many characters as possible. So this will match the beginning of every line up until the first colon

If you're able to use other tools besides grep, here are other generally better ways of doing it:

cut -d: -f1 /etc/passwd
sed 's/:.*//' /etc/passwd
awk -F: '{print $1}' /etc/passwd

You can redirect the results from any of those into allusers using > allusers like you have in your example

share|improve this answer
    
link italic bold `Thank you so much for answering my question. However, is there anyway that I can use pipe in this command?' –  guest12 Feb 19 '13 at 3:51
    
@guest12 There's no need for one, but grep can read stdin instead of a file, so you could do cat /etc/passwd | grep -oE '^[^:]+' instead I suppose –  Michael Mrozek Feb 19 '13 at 3:53
    
Re @MichaelMrozek's comment, you could throw in a sort(1) to get them in alphabetical order. Useless Use of Cat, or UUC is considered bad taste in Unixland. –  vonbrand Feb 19 '13 at 4:00
6  
While this is correct and a good answer, I wouldn't have gone beyond hints, since this was OP's homework –  jordanm Feb 19 '13 at 4:22

Grep is not suitable for this situation, try to get the password file with getent and parse it with awk (colon separated content),

getent passwd | awk -F ':' '{print $1}' > allusers

share|improve this answer
    
Thanks, I will give it a try –  guest12 Feb 19 '13 at 4:01

cat /etc/passwd | awk -F : '{print $1}' > alluser.txt

share|improve this answer
1  
cat | awk ? Wow, impressive :/ –  sputnick Feb 19 '13 at 11:50
grep -o '^[a-zA-Z]*:' /etc/passwd

works for me..it only prints the user names whose names begins with a-zA-Z ...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.