LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   understand -w option with grep (https://www.linuxquestions.org/questions/programming-9/understand-w-option-with-grep-622505/)

geeyathink 02-20-2008 09:02 AM

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,

geeyathink 02-20-2008 11:03 AM

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.

pixellany 02-20-2008 01:04 PM

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

?

geeyathink 02-20-2008 04:03 PM

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,

sundialsvcs 02-20-2008 09:13 PM

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.

geeyathink 02-21-2008 04:17 AM

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.


All times are GMT -5. The time now is 07:54 AM.