Search for Whole Words Only in Files


-w

Think back to the earlier section "The Basics of Searching Inside Text Files for Patterns," in which you first learned about grep. You searched for the word pain, and grep obediently returned a list showing you where pain had been used.

$ grep pain * fiery inferno in space.txt:watch the paint peel, three_no_more_forever.txt:all alone and in pain the speed of morning.txt:of a Chinese painting. 8 hour a day.txt:nice paint job too ghost pain.txt:Subject: ghost pain 


By default, grep searches for all occurrences of the string pain, showing you lines that contain pain, but also paint and painting. If painless, Spain, or painstaking had been in one of the files that were searched, those lines would have shown up as well. But what if you only wanted lines in which the exact word pain appeared? For that, use the -w (or --word-regexp) option.

$ grep -w pain * three_no_more_forever.txt:all alone and in pain ghost pain.txt:Subject: ghost pain 


This option can really help narrow your search results when you receive too many to easily sort through.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net