1844208 Members
2737 Online
110229 Solutions
New Discussion

Re: Grep qustion

 
Manuel_8
Advisor

Grep qustion

Hello guys,
I have a file whit 3 names inside, mmarano smmarano mmarano1.
I must find only mmarano, not smmarano and mmarano1.
I use grep to serch, but I see all the names, I try whit grep "^mmarano$", but this command not function.
I'm sure that somebody can help me.....I hope.

Thenks

Manuel
14 REPLIES 14
Hai Nguyen_1
Honored Contributor

Re: Grep qustion

Manuel,

James F.'s method should work.

Hai
James R. Ferguson
Acclaimed Contributor

Re: Grep qustion

Hi (again) Manuel:

This is clearer:

# grep mmarano filename| grep -v smmarano|grep -v mmarano1

That is, is your file. No assumptions are made as to where one any line(s) the tokens appear.

The 'grep -x mmarano' form suggested above and the regular expression ^mmarano$ accomplish the same thing -- they assume that the entire input line matches the string or regular expression.

Regards!

...JRF...
Robert-Jan Goossens
Honored Contributor

Re: Grep qustion

Hi Manuel,

# grep mmarano file
ore
# cat file | grep mmarano

Robert-Jan.
H.Merijn Brand (procura
Honored Contributor

Re: Grep qustion

1. Look at if the different regex exploitation of egrep can help
2. use word bounds in regex engines that support it (why do I always think in perlish styles ? :)

# perl -ne'/\bmmarano\b/ or print' datafile
Enjoy, Have FUN! H.Merijn
Scott_130
Occasional Advisor

Re: Grep qustion

Give this a try

cat | grep '^mmarino$'
Pete Randall
Outstanding Contributor

Re: Grep qustion

Manuel,

Try grep -x mmarano.

Pete

Pete
John Meissner
Esteemed Contributor

Re: Grep qustion

funny - when I try the same thing it works perfectly for me
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Grep qustion

Oh I see - are you just typing grep "^mmarano$"...
no i was typing cat file | grep "^mmarano$"
that works for me.
All paths lead to destiny
Pete Randall
Outstanding Contributor

Re: Grep qustion

John,

Me, too.

Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Grep qustion

Hi:

Here's one simple way"

# cat /tmp/grep|grep mmarano|grep -v smmarano|grep -v mmarano1

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Grep qustion

The easiest way (of course) is to use GNU grep with the -w option:

-w, --word-regexp force PATTERN to match only whole words

# grep -w mmarano infile
Enjoy, Have FUN! H.Merijn
harry d brown jr
Honored Contributor

Re: Grep qustion

grep -e "^mmarano$" -e " mmarano$" -e "^mmarano " -e " mmarano "

live free or die
harry
Live Free or Die
Jose Mosquera
Honored Contributor

Re: Grep qustion

Hi,

Use -x (eXact) option, this Matches are recognized only when the entire input line matches the fixed string or regular expression.

grep -x mmarano file

Of course, to do this between "mmarano" must exist al least blank spaces that limit the name like a one word.

I've tried with "grep ^mmarano file" and works fine!

If you have an inmediate charater delimiting mmarano name, you will use it into the match string, i.e. if you have a ":" character after "mmarano" string, you can specify exactly by: "grep mmarano: file"

Rgds.
Chia-Wei
Advisor

Re: Grep qustion

grep " mmarano"