0

I have a file called F1. F1 contains

Hello abc@
aaa ddd

Now, I want to check the word abc is in file F1.

After running the following command

find $dir -type f -exec egrep -w "abc" {} \;

This is the output I get

Hello abc@

For some reason the word abc@ was also found. (I was looking for abc.)

Zombo
  • 1
  • 62
  • 391
  • 407
David Lesner
  • 11
  • 2
  • 6

3 Answers3

4

This should do it:

egrep '(^| )abc( |$)' F1

It looks for abc surrounded by either space or line beginning/ending.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

You're likely using GNU egrep - the -w option isn't part of the POSIX standard - and its manual page states

-w, --word-regexp
      Select  only  those  lines  containing  matches  that form whole
      words.  The test is that the matching substring 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.

So @ is considered a non-word constituent character, just like , and . and whitespace.

If you have an idea of what you'd like the word separators to be, let us know and we can craft a regexp for you.

Zombo
  • 1
  • 62
  • 391
  • 407
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
  • You absolutely right. I know that '@' is a non-word. Space is the word separator. – David Lesner Dec 25 '13 at 23:35
  • So it's telling you that there's a match because there is one. You are looking for the word "abc". Since `@` is not considered part of a word, it sees `abc@` as the word "abc" that you're looking for - incdentally followed by a `@`, but that doesn't change anything. So what exactly would constitute a match for you? – Mark Reed Dec 25 '13 at 23:37
  • David, if your file had the strings `abc.`, `abc!`, `abc#`, `abc%`, `abc*`, `abc,`, `abc-`, and `abc?`, which of those would you want `egrep -w` to find and which would you want it to ignore? – Mark Plotnick Dec 25 '13 at 23:42
  • I don't know Mark. All I know is that I want to be able to find out by given two inputs: a filepath and a word, weather or not the word is in the given file. – David Lesner Dec 25 '13 at 23:43
  • this should work find $dir -type f -exec grep -v [[:punct:]] {} \; | grep abc – Tharanga Abeyseela Dec 25 '13 at 23:46
0
find $dir -type f -exec grep -v [[:punct:]]  {} \; | grep -ow abc
Tharanga Abeyseela
  • 3,255
  • 4
  • 33
  • 45