Operating System - HP-UX
1822708 Members
3558 Online
109644 Solutions
New Discussion юеВ

how to grep for exact word

 
Irman Surani
Frequent Advisor

how to grep for exact word

hi,

i would like to find out if it is possible to use grep to search for exact word in a file.

eg. if i use grep ab filename, it will display ab, abc, abcd, etc. but i only want to search for just ab.

Any advice ? Thanks in advance.

Best Regards,
Irman
7 REPLIES 7
KapilRaj
Honored Contributor

Re: how to grep for exact word

grep -w word

kaps
Nothing is impossible
dsrao
Occasional Contributor

Re: how to grep for exact word

Hi,

try this

grep -E -l word

bye
Animesh Chakraborty
Honored Contributor

Re: how to grep for exact word

Hi Imran,
Try grep -x "word" filename
Did you take a backup?
Irman Surani
Frequent Advisor

Re: how to grep for exact word

hi guys,

tried all the advice but not working. however, i fixed my problem my adding a space behind the word i am searching for, ie. grep "ab " filename.

thanks for the reply.

best regards,
irman
Heiner E. Lennackers
Respected Contributor

Re: how to grep for exact word

You may try this:

egrep "(^|[^a-zA-Z0-9])WORD($|[^a-zA-Z0-9])"
The reverted character class [^.....] should hold all possible character for your WORD. If you use other program (like perl) instead of the unox grep command, there may be abbrevations for this string.

Heiner
if this makes any sense to you, you have a BIG problem
Chia-Wei
Advisor

Re: how to grep for exact word

Try adding a space before and after the word you intend to search!
Chia-Wei
Advisor

Re: how to grep for exact word

Try adding a space before and after the word you inted to search!