Operating System - Linux
1748000 Members
4902 Online
108756 Solutions
New Discussion юеВ

grep - searching exact pattern

 
Anil
Advisor

grep - searching exact pattern

Hi,

Could anyone help me to search exact pattern using command "grep". -w option is not available on hp-ux 11.00. Also \ ,is not working .

Could someone please help me on this.

Thanks,
Anil
14 REPLIES 14
H.Merijn Brand (procura
Honored Contributor

Re: grep - searching exact pattern

grep -F or fgrep is your friend

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Oviwan
Honored Contributor

Re: grep - searching exact pattern

Hey

what about grep -e ....

An alternative would be perl or awk.

Regards
Dennis Handly
Acclaimed Contributor

Re: grep - searching exact pattern

>search exact pattern using command "grep".

You want to search for "words"?
The poor man's replacement for grep -w is:
$ grep -e "[[:space:][:punct:]]word[[:space:][:punct:]]"

This may not work for beginning and ending of lines so you may have to add:
-e "^word[[:space:][:punct:]]" -e "[[:space:][:punct:]]word$"


>\, is not working

This only works for vi and ex(1).
H.Merijn Brand (procura
Honored Contributor

Re: grep - searching exact pattern

Or even much better: install GNU grep. That does support -w

I have a precompiled version on my site:

http://www.cmve.net/~mbrand/greps-pa2.0.tbz

Contains GNU grep-2.5.1 (with PCRE support), agrep, and pcregrep 6.4

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Vadim Loginov
Advisor

Re: grep - searching exact pattern

Hi Anil,

You can try:
grep -x

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

I'm using it on HP-UX 11i

Regards,

Vadim
Anil
Advisor

Re: grep - searching exact pattern

thanks for the immediate response.

I have tried options -F, -x , still doesn't solve my issue. Since my server is in Production, its difficult to install the grep utility.

my requirement is to search pattern "exit"(exactly the same, should not be "#exit") in a file. It can be at any position in the file.

Please guide me..

Thank you,
James R. Ferguson
Acclaimed Contributor

Re: grep - searching exact pattern

Hi Anil:

If you want to use 'grep' then Dennis's approach will being you the closest to your needs.

However, you need to _define_ what you mean by a "word". Perl offers one easy way to do this. For example to find the token "localhost" in '/etc/hosts' without regard to case and bracketed by non-word characters (including line endings or beginnings):

# perl -nle 'print if /\blocalhost\b/i' /etc/hosts

If you want case-sensitivity, drop the "i" from the pattern to match.

Regards!

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

Re: grep - searching exact pattern

And you do not have to 'install' grep.
Just put it in your own $HOME/bin or so.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
HGN
Honored Contributor

Re: grep - searching exact pattern

Hi

If you feel that this issue is resolved go-ahead and assign points for the people who have helped you that is a nice way to show gratitude for the people who helped you. You have assigned points only to 3 of 40 responses.

Rgds

HGN