Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I grep a keyword which contain underscore?

Tags:

bash

I am currently searching some keyword something like

find -type f | xargs -grep -i -w 'weblogic_*'

But it show all the keyword match with weblogic instead of weblogic_

like image 336
taymedee Avatar asked Apr 30 '14 03:04

taymedee


People also ask

How do you grep a symbol?

grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".

What is grep _E?

According to man grep : -E, --extended-regexp Interpret pattern as an extended regular expression (i.e. force grep to behave as egrep). -e pattern, --regexp=pattern Specify a pattern used during the search of the input: an input line is selected if it matches any of the specified patterns.

How do I grep a string with a dot?

In grep, a dot character will match any character except a return. But what if you only want to match a literal dot? If you escape the dot: "\.", it will only match another literal dot character in your text.

How do you grep double dash?

The double dash ( -- ) signals the end of options for that particular command (the end of the command line flags), after which only positional arguments are accepted. This way, grep doesn't try to interpret what follows the double dash as an option/flag.


2 Answers

grep uses regular expressions, not globs (wildcard expressions).

In regular expressions, * is a quantifier that relates to the previous character or expression. Thus, _* is saying: zero or more instances of _, so NO _ will be matched as well.

You probably want:

'weblogic_.*'

which states that any (.) character may follow the _ zero or more times.

Note, however, that ending your regex in _.* partially contradicts grep's -w flag in that it will now only match the beginning of your regex on a word boundary.

If you wanted to be more explicit about this, you could use the word-boundary assertion \b and drop the -w option:

'\bweblogic_'

As you can see, this allows you to omit the .*, as grep performs substring matching by default, and you needn't match the remainder of the line if it is not of interest.

Also, there is no need for xargs: it is simpler and more efficient to use find's -exec primary, which has xargs built in, so to speak:

 find . -type f -exec grep -i '\bweblogic_' {} +

{} represents the list of input filenames and + specifies that as many input filenames as possible should be passed at once - as with xargs.

Finally, if your grep version supports the -R option, you can make do without find altogether and simply let grep process all files recursively:

 grep -R -i '\bweblogic_' .
like image 75
mklement0 Avatar answered Sep 23 '22 01:09

mklement0


When you use the pattern weblogic_*, it means look for weblogic followed by zero or more occurrences of _.

You can change it to use the pattern weblogic_.* if you want to avoid matching weblogic that is not followed by a _.

find -type f | xargs -grep -i -w 'weblogic_.*' 

should work.

like image 38
R Sahu Avatar answered Sep 23 '22 01:09

R Sahu