2013-03-20 23 views
9

我試圖搜索當前目錄中的特定單詞BML.I如果字符串已經有點,grep的確切單詞

當我用下面的命令嘗試:

grep -l "BML.I" * 

它顯示了所有的結果,如果它包含單詞BML

是否有可能到grep的精確匹配BML.I

+0

您是否在尋找文件名或文件內容? – Alepac 2013-03-20 11:02:40

+0

@Alepac顯然他在尋找內容。 – Kent 2013-03-20 11:10:20

+0

只讀'man grep'。 – 2013-03-20 11:11:56

回答

15

你需要逃避的。 (句點),因爲默認情況下它與任何字符匹配,並指定-w以匹配特定的詞,例如

grep -w -l "BML\.I" * 

注意上面有兩級轉義。引號確保shell將BML\.I傳遞給grep。然後\轉義爲grep。如果省略了引號,然後殼解釋\作爲期間逃逸(並會簡單地傳遞轉義期間grep

+0

您可能需要單引號。 – filmor 2013-03-20 11:02:58

+0

我*可能會這樣做,但是在表達式 – 2013-03-20 11:07:49

+0

中沒有解釋/展開的變量,這將匹配不是「特定單詞」BML.I「' – Kent 2013-03-20 11:08:30

7

嘗試grep -wF

從手冊頁:

-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. 



-F, --fixed-strings 
       Interpret PATTERN as a list of fixed strings, separated by newlines, any 
       of which is to be matched. (-F is specified by POSIX.) 
4

我使用fgrep,與grep -F相同

相關問題