Is it a fake website?

15 enero 2007

Grep utility question

Tenemos un archivo llamado nombres.txt con las siguientes entradas:

aperez.2
aperez_
apepito

Queremos realizar una búsqueda exacta de aperez, que no tendrá que aparecer en este archivo.
Pero voilá;

[root@server tmp]# grep -w aperez nombres.txt
aperez.2

¿Porqué aparece?

El man del grep dice:
-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.

¿Cómo evitar que aparezca?

2 comentarios:

Unknown dijo...

egrep -o '\<aperez[^[:punct:]]'

-o Muestra lo coincidente
\< Comienzo de una palabra
[^[:punct:]] Rechaza los signos de puntación

FELICITACIONES!!! por el Blog, MANTENLO

Emilio dijo...

Javier Enrique, Gracias por tu apunte!
Buena solución con las expresiones de los paréntesis rectos.
Aprovecho y hago este apunte:
[:alnum:] alfanuméricos
[:alpha:] alfabéticos
[:cntrl:] de control
[:digit:] dígitos
[:graph:] gráficos
[:lower:] minúsculas
[:print:] imprimibles
[:punct:] de puntuación
[:space:] espacios
[:upper:] mayúsculas
[:xdigit:] dígitos hexadecimales

[:digit:] equivale a [0-9]
pero la segunda expresión depende de la codificación ASCII la primera es más portable.

Gracias por las felicitaciones, animan de verdad. ;)