Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

finding only whole word in grep

Status
Not open for further replies.

bcdixit

Technical User
Nov 11, 2005
64
0
0
US
How do I find only specific word in a file using grep.
for eg.
I want to find the word 'PRODUCT' (case insenstive)

I used the -i switch to ignore case and -w to match the whole word 'product'.

But I find matches that are
product
PRODUCT
PRODUCT_..

I think the '_'(underscore character) is considered as a word character. How do I make grep to igonre the underscore character.

Please Help.
Thanks
bcdixit
 
A crude workaround:
tr '_' 'A' < /path/to/file | grep -i -w product

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
thanks..but i am actually using grep in a find command. i.e. i am looking for mutiple files in a directory with that particular keyword
 
Any chance you could post your actual code ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
find . -name "*" -exec grep -qwi "product" '{}' \; -print>result.txt

basically, i want the list of file names in a file result.txt which have the keyword only "product".

some of the files have "product_" or "_product" etc etc
 
find . -type f | while read f; do tr '_' 'A' < $f | (grep -qwi product && echo $f); done > result.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
can you explain this code for me before I go and try it out .
thanks
 
Strange, none of my greps seem to match "product_" with a grep -w.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top