1

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 fileI want to display all users that contain more than one letter 'o' in another file

Anthon's user avatar
Anthon
81.4k42 gold badges174 silver badges228 bronze badges
askedOct 14, 2013 at 17:27
5
  • Huh? How would you sort that? Can you give us an example input and desired output?CommentedOct 14, 2013 at 17:32
  • So how would you want to sort it exactly? Please give an example input and output.CommentedOct 14, 2013 at 17:48
  • You might want to merge your accounts. That way you can edit this post directly without needing approval. Seehere.CommentedOct 14, 2013 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!CommentedOct 14, 2013 at 17:57
  • For the names you want, do you mean those that end in 'o' or those that contain 'oo'?CommentedOct 15, 2013 at 4:12

2 Answers2

2

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

get names that contain more than oneo:

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

or

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

Or, usinggrep (which is silly in this case) andsed to remove the trailing colon:

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

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

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

get names that contain more than oneo and end ino:

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

or

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.txtcut -d':' -f 1 /etc/passwd | grep '.*o.*o.*' > last_o.txt
answeredOct 14, 2013 at 17:50
terdon's user avatar
1
  • 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.CommentedOct 14, 2013 at 17:53
1

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 perluse 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"; }}
terdon's user avatar
terdon
253k69 gold badges481 silver badges719 bronze badges
answeredOct 15, 2013 at 4:27
ChuckCottrill's user avatar

You mustlog in to answer this question.