Like grep for the whole word

I use the following command for grep stuff in subdirs

find . | xargs grep -s 's:text' 

However, it also finds things like <s:textfield name="sdfsf"...../>

What can I do to avoid this, so it just finds things like <s:text name="sdfsdf"/>

OR, for that matter ... also finds <s:text somethingElse="lkjkj" name="lkkj"

basically s:text and name should be on the same line ....

+42
unix grep
May 21 '10 at 1:41
source share
5 answers

You want the -w option to indicate that this is the end of a word.

find . | xargs grep -sw 's:text'

+48
May 21 '10 at 1:47 a.m.
source share

You can refuse the xargs command by doing a grep search recursively. And you usually don't need the 's' flag. Hence:

 grep -wr 's:text' 
+22
Jul 03 '13 at 10:33
source share

Use \b to match word boundaries, which will make your search match only for whole words.

So your grep would look like

 grep -r "\bSTRING\b" 

adding colors and line numbers can also help

 grep --color -rn "\bSTRING\b" 

From http://www.regular-expressions.info/wordboundaries.html :

There are three different positions as word boundaries:

  • Before the first character in a string, if the first character is a word character.
  • After the last character in a line, if the last character is a word character.
  • Between two characters in a string, where one is a word symbol and the other is not a word symbol.
+19
Dec 03 '15 at 19:25
source share

If you want to filter out a portion of the remainder text, you can do this.

xargs grep -s 's:text '

This should only find instances of s:text with a space after the last t. If you need to find instances of s:text that have only a name element, either pass the results to another grep expression, or use a regular expression to filter only those elements that you need.

0
May 21 '10 at 1:47 a.m.
source share

you can try rg, https://github.com/BurntSushi/ripgrep :

 rg -w 's:text' . 

gotta do it

-one
Oct 30 '17 at 10:51 on
source share



All Articles