LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-20-2008, 10:02 AM   #1
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Rep: Reputation: 15
understand -w option with grep


I had wanted to include some abbreviations in my searches. cu, al and maybe a few more. I have not been able to get them to match only whole words ie "cu" and "al" They seem to match "cut" "all" "alabama' "cucumber" etc..

Is it correct that the -w option with grep would cause the pattern "cu" to not return a match on "cut".
To use the -w option with grep do I just include it along with any other options at the beginning?
I did not see in man where '-woic -f patternfile file' should conflict with one another.

How far off base am I this time?

I can include code or more context if needed. My script is getting big. I would only post what I hope is the appropriate section if that would do.

Thanks very much,
 
Old 02-20-2008, 12:03 PM   #2
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
I had a few minutes to mess with this at a command line

If I put the word I expect to return "garden" then follow it by the other words that should not be in the file "al" "cu"
Code:
[teabear@junker ~]$ grep -iow -e "garden" -e "cu" -e "al"  /home/teabear/desktop/ok/tster
Garden
Garden
Garden
Garden
Garden
Garden
Garden
Garden
Garden
al
It returns more like I thought it would

But if I reverse the order of the patterns
Code:
[teabear@junker ~]$ grep -iow -e "cu" -e "al" -e "garden" /home/teabear/desktop/ok/tster
al
al
al
Garden
Garden
al
Garden
cu
Garden
Garden
Garden
al
al
It returned a load of the "al" and "cu"

If I seach without the word I was expexting to return
Code:
[teabear@junker ~]$ grep -iow -e "cu" -e "al"  /home/teabear/desktop/ok/tster
[teabear@junker ~]$
It returns nothing at all

tster is a html file about gardening, I just changed the long name it had to tster.

I get very similar results with a plain text file.

I fear it may be time for me to understand regular expressions. Hopefully not yet, just figuring how to use
these plain commands would be great for now if possible.
 
Old 02-20-2008, 02:04 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
I don't think there should be any issue combining those options.

Note:
Instead of:
grep -e cu -e al filename (the quotes are not needed)

why not:
egrep 'cu|al' filename

?
 
Old 02-20-2008, 05:03 PM   #4
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
I think I created a small html file with kompoze, to have better control over testing this out.

It only has seven words in it:

linux computer network
cucumber dual almost cups

I got the same behaviour as in the above post when I changed to egrep 'pattern|pattern|pattern' etc...

Anything else anyone thinks for me to look into is great

Thank you,
 
Old 02-20-2008, 10:13 PM   #5
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,455

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
The essential purpose of "-w" is to say that, in order to "match," the specified pattern must be both preceded and followed by a non-word character (or the beginning/end of line).

I don't use "-w" in any situation more complicated than that.
 
Old 02-21-2008, 05:17 AM   #6
geeyathink
Member
 
Registered: Apr 2006
Distribution: Mandy 2007, Kubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
Thanks sundialsvcs,

Yes that is I believe all I want out of it

here is output of that seven word file with -ow options

Code:
[teabear@junker ~]$ egrep -ow  'networ|computer|linux|cu|al'  /home/teabear/desktop/tsthtml
linux
computer
networ
al
al
cu
cu
cu

here it is with only the -w option

Code:
[teabear@junker ~]$ egrep -w  'networ|computer|linux|cu|al'  /home/teabear/desktop/tsthtml
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"><title>mmmam</title></head><body>linux computer network<br><br>dual almost cucumber cups</body></html>
[teabear@junker ~]$

The color is missing but in the second output the searched terms are highlighted. Maybe the html stuff is is interfering?

I a going to go double check with plain text file

Yes same behaviour with a text file,
Code:
[teabear@junker ~]$ egrep -w  'computer|linux|cu|al'  /home/teabear/desktop/testtext
linux computer aluminum cucumber
aluminum cucumber linux computer
[teabear@junker ~]$

Code:
[teabear@junker ~]$ egrep -wo  'computer|linux|cu|al'  /home/teabear/desktop/testtext
linux
computer
al
cu
cu
al
cu
cu
linux
computer
[teabear@junker ~]$
so it isn't just the html format thats messing me up

What could I have screwed up to be causing this? Some kind of envirioment
setting?

Thanks again to all,



I reinstalled only my / and still had the problem, tried a while longer to figure out the prob, gave up and then reinstalled /home and now the problem is gone.

I suspect that during my experimenting with grep,sed,awk etc... I accidently screwed up a config file that bash looks at. Just a guess. Don't put much stock in it. I have yet to be correct about anything System or Programming related.

Last edited by geeyathink; 03-01-2008 at 09:10 AM. Reason: for future searchers
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Paragraph-option to grep TheSpork Linux - General 2 03-17-2006 02:45 AM
Help me to understand this command: find...| grep -e ... | xargs rm -f ROBERT483599 Red Hat 2 02-14-2006 09:21 AM
How to use "OR" option in a grep command. sumitarun Programming 3 04-18-2005 02:44 AM
grep option needed ziox Linux - General 7 01-05-2005 12:02 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM


All times are GMT -5. The time now is 03:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration