The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to grep for a word and display only the word ananthmm UNIX for Dummies Questions & Answers 6 05-29-2008 05:00 AM
Grep for Particular Word reddi22 Shell Programming and Scripting 2 01-22-2008 07:36 AM
grep a word from a line Orbix UNIX for Dummies Questions & Answers 2 12-23-2007 07:52 AM
Grep for X character on a word jjoves UNIX for Dummies Questions & Answers 5 08-06-2004 02:14 AM
How to grep more then one word? roco UNIX for Dummies Questions & Answers 5 10-18-2002 07:43 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-22-2008
Registered User
 

Join Date: Jul 2007
Posts: 54
Stumble this Post!
grep for word

Hi, I'm a little confused with the grep options and can't seem to find the correct one I need. I basically want to grep a file for a word with the pattern in it. I tried:

Code:
grep -w word file
But this just returns word if it's in there somewhere. I want the full word that that pattern is in so if my file looks like

this is a file
and here is someword

I want it to return:

someword
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-22-2008
DukeNuke2's Avatar
Soulman
 

Join Date: Jul 2006
Location: Germany, Berlin
Posts: 1,303
Stumble this Post!
work with returncodes...

Code:
grep -w yourword yourfile 2>/dev/null
if [ $? -eq "0" ]
then
 echo "yourword found"
else
 echo "yourword NOT found"
fi
do a little finetuning on the above "script" to fit your needs...

hth,
DN2
Reply With Quote
  #3 (permalink)  
Old 04-22-2008
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,211
Stumble this Post!
Quote:
Originally Posted by eltinator View Post
Hi, I'm a little confused with the grep options and can't seem to find the correct one I need. I basically want to grep a file for a word with the pattern in it. I tried:

Code:
grep -w word file
But this just returns word if it's in there somewhere. I want the full word that that pattern is in so if my file looks like

this is a file
and here is someword

I want it to return:

someword
Add the -o option.
Quote:
-o, --only-matching
Show only the part of a matching line that matches PATTERN.
-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching sub-
string must either be at the beginning of the line, or preceded by a non-word constituent character.
Similarly, it must be either at the end of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the underscore.
Code:
grep -ow pattern inpufile
Jean-Pierre.
Reply With Quote
  #4 (permalink)  
Old 04-22-2008
Registered User
 

Join Date: Jul 2007
Posts: 54
Stumble this Post!
hmm the -ow option didnt seem to work. I tried changing the ordering of it too but it didnt return anything at all. Do I need to pipe it or anything?
Reply With Quote
  #5 (permalink)  
Old 04-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
grep -ow word will find "word" but not "someword". A pattern which matches the entire word in which the search pattern is embedded would be something like

Code:
grep -ow '[A-Za-z]*word[A-Za-z]*' file
This says if there are any A-Z or a-z characters before or after, those are included in the pattern (and thus in what should be printed by -o). Then I guess you don't really need the -w option because the specified pattern already takes care of matching an entire word.

If you want "quote" marks, 'single' quotes, hyp-hens or abbr'viation apostrophes, you need to change the pattern slightly. Getting it perfectly right for all possible English words is not easy (and perhaps not even doable, depending on how you define "all", "possible", and "English").
Reply With Quote
  #6 (permalink)  
Old 04-22-2008
Registered User
 

Join Date: Jul 2007
Posts: 54
Stumble this Post!
cool thanks for the help =D
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0