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, |
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 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 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 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. |
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 ? |
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, |
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. |
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 here it is with only the -w option Code:
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 Code:
[teabear@junker ~]$ egrep -wo 'computer|linux|cu|al' /home/teabear/desktop/testtext 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. |