1833785 Members
4014 Online
110063 Solutions
New Discussion

Newbie question - Grep

 
SOLVED
Go to solution

Newbie question - Grep

I'm trying to use grep on file, a list of usernames.
I want to get out all the usernames with 6 characters in them.

Can some one please help me out with the syntax.

So far i have tried -

cat file|grep ??????

also tried enclosing the ?'s in quotes, but that didn't work.

I have looked at the man for grep but i cannot see anything that might work ....
4 REPLIES 4
Mark Vollmers
Esteemed Contributor
Solution

Re: Newbie question - Grep

Mark-

You might have a problem just generically searching for any 6 char items in the list. You might be able to use "grep '......' filename", which will look for any entries with any characters (the . is any one character), but this will probably grab every line in filename, since spaces might be included. You can use special characters to search the beginning of the line or to limit by looking at letters only, caps, numbers, etc. Can you be more specific about the file? Grep is really powerful, but getting the regular expressions down can be a pain.

Mark
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"

Re: Newbie question - Grep

Thanks for that Mark.
Since the file i'm using is a straight list of usernames, each one on a seperate line there are no worries about spaces ...

So if i use -

cat file|grep '......'|grep -v '.......'

That gives me what i want :O)

Once again thanks :O)
Mark Vollmers
Esteemed Contributor

Re: Newbie question - Grep

Mark-

I think I have found an expression that will work, assuming that the user names in the file are all the first items on the lines and have info after them. Try

grep '^[A-Za-z]\{6\}[ ]' filename

This should look at the beginning of each line (^) for any line with 6 lower or upper case alpha characters ([A-Za-z]\{6\}) followed by a space ([ ]). Hope this helps.

Mark
"We apologize for the inconvience" -God's last message to all creation, from Douglas Adams "So Long and Thanks for all the Fish"
Rodney Hills
Honored Contributor

Re: Newbie question - Grep

Here are some other ways to do it...

awk 'length($0)==6{print $0}' filename
perl -ne 'print if length == 7' filename
sed -ne '/^......$/p' filename
There be dragons...