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
- Huh? How would you sort that? Can you give us an example input and desired output?2013-10-14 17:32:01 +00:00CommentedOct 14, 2013 at 17:32
- So how would you want to sort it exactly? Please give an example input and output.2013-10-14 17:48:01 +00:00CommentedOct 14, 2013 at 17:48
- 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!2013-10-14 17:57:50 +00:00CommentedOct 14, 2013 at 17:57
- For the names you want, do you mean those that end in 'o' or those that contain 'oo'?ChuckCottrill– ChuckCottrill2013-10-15 04:12:58 +00:00CommentedOct 15, 2013 at 4:12
2 Answers2
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/passwdrootnobodycolordfooor
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- 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– Jeight2013-10-14 17:53:02 +00:00CommentedOct 14, 2013 at 17:53
I think he means end in 'o' OR contain "oo",
cut -d: -f1 /etc/passwd | grep -E "oo|o$" |sortBut 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 |uniqAnd 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"; }}
