2013-03-20 10 views
9

Stavo cercando una parola specifica BML.I in una directory corrente.Come grep per la parola esatta se la stringa contiene il punto

Quando ho provato con il seguente comando:

grep -l "BML.I" * 

Si sta visualizzando tutti i risultati se contiene la parola BML

E 'possibile grep per la corrispondenza esatta BML.I

+0

Stai cercando nomi di file o il contenuto del file? – Alepac

+0

@Alepac ovviamente sta cercando contenuti. – Kent

+0

Basta leggere "man grep'. –

risposta

15

È bisogno di sfuggire al. (punto) poiché per impostazione predefinita corrisponde a qualsiasi carattere e specifica -w per far corrispondere una parola specifica, ad es.

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

Nota ci sono due livelli di escape in quanto sopra. Le virgolette assicurano che la shell passi a BML\.I in grep. Il \ quindi sfugge il periodo per grep. Se si omette le virgolette, allora la shell interpreta il \ come una via di fuga per il periodo (e sarebbe semplicemente passare il periodo di escape per grep)

+0

Si potrebbe desiderare singole citazioni lì. – filmor

+0

I * potrebbe * fare, ma non ci sono variabili da interpretare/espandere nell'espressione –

+0

questo corrisponderà a 'fooBML.Ibar' che non è una parola particolare" BML.I "' – Kent

7

provare grep -wF

dalla pagina man:

-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

Io uso fgrep, che è lo stesso di grep -F

Problemi correlati