Diferença entre 'grep' \ hi \ 'coco.txt' e 'grep' oi 'coco.txt'

0

Qual é a diferença entre: grep '\<hi\>' coco.txt e grep 'hi' coco.txt ? Eu apliquei esses comandos no terminal, mas não vejo diferença.

    
por sosoma 03.11.2013 / 09:42

1 resposta

3

Em grep , \< significa início da palavra e \> significa fim da palavra .

De man grep :

The Backslash Character and Special Expressions
   The symbols \< and \>  respectively  match  the  empty  string  at  the
   beginning and end of a word.  The symbol \b matches the empty string at
   the edge of a word, and \B matches the empty string provided  it's  not
   at the edge of a word.  The symbol \w is a synonym for [_[:alnum:]] and
   \W is a synonym for [^_[:alnum:]].

Portanto, grep '\<hi\>' corresponderá a qualquer linha que contenha a palavra hi e grep 'hi' corresponderá a qualquer linha que contenha a sequência de caracteres hi :

$ grep '\<hi\>' <<< "hi"
hi
$ grep '\<hi\>' <<< "chimes"
$ # no output since no match
$ grep '\<hi\>' <<< "hi-fi"
hi-fi
$ grep '\<hi\>' <<< "high voltage"
$ # no output since no match
    
por gniourf_gniourf 03.11.2013 / 12:22