問題があります。解決策を教えてください。
以下に示すようにコマンドを実行する必要があります。これにより、「abcde1234」で指定された文字列を含むすべてのファイルが一覧表示されます。
/path/to/dir/ を見つけます * | xargs grep abcde1234
ただし、ここでは、文字列「abcde1234567」を含むファイルも表示されます。ただし、「abcde1234」という単語を含むファイルのみが必要です。コマンドでどのような変更が必要ですか??
問題があります。解決策を教えてください。
以下に示すようにコマンドを実行する必要があります。これにより、「abcde1234」で指定された文字列を含むすべてのファイルが一覧表示されます。
/path/to/dir/ を見つけます * | xargs grep abcde1234
ただし、ここでは、文字列「abcde1234567」を含むファイルも表示されます。ただし、「abcde1234」という単語を含むファイルのみが必要です。コマンドでどのような変更が必要ですか??
そのようなものが必要な場合は、単語の境界を意味する\<
andを使用します。\>
このような:
grep '\<abcde1234\>'
The symbols \< and \> respectively match the empty string at the beginning and end of a word.
しかし、それは私です。正しい方法は、代わりにスイッチを使用することかもしれません-w
(私は忘れがちです):
-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.
もう 1 つ: find
+のxargs
代わりfind
に-exec
. または、実際には次のようにgrepし-r
ます:
grep -w -r abcde1234 /path/to/dir/
$ grep abcde1234 *
abcde1234
これにより、文字列が含まれるファイル名を使用して、現在のディレクトリの文字列が grepされます。元:
abc.log: abcde1234 found
こんにちは、これで答えが出ました。検索したい単語に $ を付けると、その単語だけを含むファイルが表示されます。
コマンドはこのようになります。
/path/to/dir/ を見つけます * | xargs grep abcde1234$
ありがとう。