2013-03-25 107 views
0

我有一個問題。請給我一個解決方案。在shell腳本中搜索specefic字

我必須按照下面給出的命令運行一個命令,它將列出包含字符串「abcde1234」的所有文件。

find/path/to/dir/* | xargs的grep的abcde1234

但這裏將顯示包含字符串「ABCDE1234567」 also.But我的東東只含有單詞「abcde1234」文件的文件。我需要在命令中進行哪些修改?

+0

你可以嘗試添加單詞邊界'的grep '\ '' – RedBaron 2013-03-25 06:17:59

回答

4

當我需要類似的東西時,我使用\<\>這意味着字邊界。就像這樣:

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. 

一件事

:不是 find + xargs你可以 -exec只使用 find。或實際只是 -r用grep:

grep -w -r abcde1234 /path/to/dir/ 
+0

這helped.Thanky OU aragaer。 – user103134 2013-03-25 06:21:53

0

您好我得到了this.By $連接用字的答案被搜索,它會顯示僅包含這個詞的文件。

該命令將是這樣的。

find/path/to/dir/* | xargs grep abcde1234 $

謝謝。

+1

它將只顯示行_ending_,如果'abcd1234'不是特定行中的最後一個單詞,這將失敗。 @aragaer有正確的答案/想法 – aragaer 2013-03-25 06:22:38

1
$ grep abcde1234 * 

這將用grep串abcde1234在當前目錄下,並在該字符串的文件名。 例如:

abc.log: abcde1234 found