Sign up ×
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.

Re : grep command.

I have to search a file for the names of all the students in the school's Linux system. I am able to do this and return about 665 names. (First string of /etc/passwd file).

From there, I need to sort on the students who's name ends in 'o' and also those who have two or more 'o' in their name.

This isn't actually for me — it is indeed homework but for a friend.

I am way past the age of homework :)

He has to use grep to get the results.

I haven't used grep for years, I played a round a little but can't stay away from using other commands to achieve this. I was just curious to see if someone in the community knew how to do this using grep.

For the names in the /etc/passwd file ending in letter 'o' :

grep -Eo '^[a-zA-Z0-9._-]+' /etc/passwd | grep -Eo 'o$'

This works but doesn't display the whole name , just the letter 'o' for the one single match to the request in the file.

Not sure :

1) how to display the output , ie.. whole name ? 2) how to search for those names with more than one 'o' in their name ?

From the /etc/passwd file for example.... I want to display the output of all users ending in the letter 'o' in a file I want to display all users that contain more than one letter 'o' in another file

share|improve this question
Huh? How would you sort that? Can you give us an example input and desired output? – terdon Oct 14 '13 at 17:32
So how would you want to sort it exactly? Please give an example input and output. – terdon Oct 14 '13 at 17:48
You might want to merge your accounts. That way you can edit this post directly without needing approval. See here. – terdon Oct 14 '13 at 17:55
Oh and adding comments in the edit window is not really effective, they will only be seen by users who try to edit your post. To say thanks, upvote a helpful answer and accept one that solves your problem. Welcome to the site! – terdon Oct 14 '13 at 17:57
For the names you want, do you mean those that end in 'o' or those that contain 'oo'? – ChuckCottrill Oct 15 '13 at 4:12

2 Answers 2

I'm still not entirely sure what you're trying to do but this should help.

get names that contain more than one o:

$ awk -F':' '$1~/.*o.*o/{print $1}' /etc/passwd


cut -d':' -f 1 /etc/passwd | grep '.*o.*o.*' 

Or, using grep (which is silly in this case) and sed to remove the trailing colon:

grep -oP '^.*?:' /etc/passwd |grep '.*o.*o.*' | sed 's/://'

If you really really need to do it using grep alone, try this:

grep -oP '^.*?:' /etc/passwd |grep '.*o.*o.*' | grep -o '[^:]*'

get names that contain more than one o and end in o:

awk -F':' '$1~/.*o.*o/{print $1}' /etc/passwd | grep 'o$'


cut -d':' -f 1 /etc/passwd | grep '.*o.*o.*' | grep 'o$'

pure grep:

grep -oP '^.*?:' /etc/passwd |grep '.*o.*o.*' | grep -o '[^:]*' | grep 'o$'

save the output to different files

cut -d':' -f 1 /etc/passwd | grep '.*o.*o.*' > two_os.txt
cut -d':' -f 1 /etc/passwd | grep '.*o.*o.*' > last_o.txt
share|improve this answer
Very well done! I was in the process of writing an answer and this is much more well written and detailed than what I was going to offer. – Jeight Oct 14 '13 at 17:53

I think he means end in 'o' OR contain "oo",

cut -d: -f1 /etc/passwd | grep -E "oo|o$" |sort

But if we must use grep, rather than grep -E,

(cut -d: -f1 /etc/passwd | grep "oo"; cut -d: -f1 /etc/passwd | grep "o$" /tmp/names) |sort |uniq

And in perl,

#!/bin/env perl
use strict;
use warnings;
open(my $fh,"< /etc/passwd") || die "cannot open passwd";
while(my $line=<$fh>) {
    my @cols = split(/:/,$line);
    if( $cols[0] =~ /oo/ || $cols[0] =~ /o$/ ) { print "$cols[0]\n"; }
share|improve this answer

Your Answer


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