IT management/strategy

Unix Tip: Grepping on Whole Words

November 3, 2009, 05:55 PM — 

Whenever you use a simple grep command to find a single word or phrase in a file, you run the risk of getting a lot of extra "stuff" you didn't want to see. Grep for "not" and you get "nothing", "notes", "notary" and "notwithstanding". Grep for "hub" and you get "chubby", "hubby", "hubris" and "rhubarb". You can try looking for the word of your choice by putting blanks in front of and behind the word, but you then run the risk of missing your target text if it sits at the beginning or the end of a line. Here are two grep commands that will do just what you want.

The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.

$ grep -w hub /usr/dict/words
hub

That's much different than this:

$ grep hub /usr/dict/words
chub
chubby
hub
hubbub
hubby
hubris
rhubarb
Schubert
Thuban

You can also use the regular expression \< and \> delimiters that select whole words. Don't forget to put your expression in quotes as shown.

$ grep "\<hub\>" /usr/dict/words
hub

Either of these tricks will keep you from getting "catharsis" and "catatonic" when you only want lines containing "cat". This sure beats looking for a whole word by grepping for " cat ", "^cat " and " cat$". Besides, these options wouldn't have a chance of finding your target text if it appeared inside quotes or following by some character other than a blank while still clearly a whole word:

$ grep -w cat program.c
printf("Hello, cat\n");

The -w and \< \> options for selecting whole words also work for phrases. If you want to find instances of "I want cats" in a large file of personal aspirations while avoiding "I want catsup", either of these commands would work just fine:

$ cat wish-list
I want my treehouse to be completed
I want a nap
I want piles of money
I want a vacation
I want some new friends
I want cats
I want catsup
I want it all
$ grep cats wish-list
I want cats
I want catsup
$ grep -w "I want cats" wish-list
I want cats
$ grep "\<I want cats\>" wish-list
I want cats

The grep command's -w option is explained in the man page as searching for "the expression as a word as if surrounded by \< and \>" and it does just that.

If you want your finds to include the line number, just add -n as in:

$ grep -nw cats wish-list
6:I want cats
$ grep -nw cats *
wish-list:6:I want cats

If you don't want to see file names when you grep on multiple files, use -h:

grep -wh cats *
I want cats

The grep command is a lot more versatile than I remembered. It pays to read man pages now and then. These are very useful options!

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325

Join the conversation here

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

Marketplace