Reputation: 741
I have a problem.Please give me a solution.
I have to run a command as I given below, which will list all the files that contain the string given "abcde1234".
find /path/to/dir/ * | xargs grep abcde1234
But here it will display the files which contain the string "abcde1234567" also.But I nee only files which contain the word "abcde1234". What modification shall I need in the command ??
Upvotes: 0
Views: 390
Reputation: 3938
$ grep abcde1234 *
This will grep the string abcde1234
in current directory, with the file name in which the string is.
Ex:
abc.log: abcde1234 found
Upvotes: 1
Reputation: 741
Hi I got the answer for this.By attaching $ with word to be searched, it will display the files that contain only that word.
The command will be like this.
find /path/to/dir/ * | xargs grep abcde1234$
Thanks.
Upvotes: 0
Reputation: 17848
When I need something like that, I use the \<
and \>
which mean word boundary. Like this:
grep '\<abcde1234\>'
The symbols \< and \> respectively match the empty string at the beginning and end of a word.
But that's me. The correct way might be to use the -w
switch instead (which I tend to forget about):
-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.
One more thing: instead of find
+ xargs
you can use just find
with -exec
. Or actually just grep with -r
:
grep -w -r abcde1234 /path/to/dir/
Upvotes: 4