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_
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 " | ".
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.
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.
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.
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_' .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With