grep
Busca uno o varios patrones en ficheros.
Sintaxis:
$ grep [OPCIONES] PATRON FICHERO(S)
Opciones:
- -i : No distingue entre mayúsculas y minúsculas.
- -c : Cuenta el número de coincidentes.
- -e : Especifica un patrón.
- -v : Búsqueda inversa.
- -A NUMERO : Muestra número de líneas a continuación.
- -B NUMERO : Muestra el número de líneas anteriores.
- -r/R : Búsqueda recursiva.
- -H : Incluye nombre de fichero.
- -h : Omite nombre del fichero.
Ejemplos de uso:
$ cat oficina.txt
Jefe:
Pedro
Ventas:
Antonio
Ramon
Informaticos:
Luis
Carmen
Maria
$ grep : oficina.txt
Jefe:
Ventas:
Informaticos:
$ cat Ma oficina.txt
Maria
$ cat -i Ma oficina.txt
Informaticos:
Maria
$ cat -c -i Ma oficina.txt
2
Para buscar por varios patrones:
$ grep -i -e MA -e : oficina.txt
Jefe:
Ventas:
Informaticos:
Maria
Para buscar no iguales...
$ grep -v : oficina.txt
Pedro
Antonio
Ramon
Luis
Carmen
Maria
Para mostrar las líneas siguientes a nuestro patrón..
$ grep Ventas -A 2 oficina.txt
Ventas:
Antonio
Ramon
Para mostrar las anteriores ...
$ grep Ventas -B 2 oficina.txt
Jefe:
Pedro
Ventas:
Buscar algo en todos los ficheros del directorio donde estemos:
$ grep Mar *
apellidos.txt:2 Martinez
cut2.txt:Maria mujer
cut.txt:Maria : 30 Jaher : mujer
grep: Dir1: Es un directorio
grep: Dir2: Es un directorio
miFicher-ab:Maria mujer
nombres.txt:4 Maria
oficina.txt: Maria
Búsqueda recursiva ...
$ grep -r Mar *
apellidos.txt:2 Martinez
cut2.txt:Maria mujer
cut.txt:Maria : 30 Jaher : mujer
Dir1/nombres.txt:4 Maria
Dir2/nombres.txt:4 Maria
miFicher-ab:Maria mujer
nombres.txt:4 Maria
oficina.txt: Maria
Para omitir el nombre del directorio...
$ grep -r -h Mar *
2 Martinez
Maria mujer
Maria : 30 Jaher : mujer
4 Maria
4 Maria
Maria mujer
4 Maria
Para mostrar el nombre del archivo ..
$ grep -H Mar oficina.txt
oficina.txt: Maria
Omitir...
$ grep -h Mar oficina.txt
Maria
egrep
Habilita las expresiones regulares extendidas, así podremos operar con ellas en un grep
. La sintaxis es la misma.
$ grep -E "^([0-9]|[0-9]{2})$" numeros.txt
NO MUESTRA NADA LE FALTA LA -->>> -E
$ grep -E "^([0-9]|[0-9][0-9]{2}$" numeros.txt
1
1
2
3
5
8
13
21
34
55
89
$ egrep "^([0-9]|[0-9]{2})$" numeros.txt
1
1
2
3
5
8
13
21
34
55
89
fgrep
Deshabilita todas las expresiones regulares tanto extendidas como las básicas. Estos dos comandos son equivalentes ambos deshabilitan las expresiones regulares...
$ grep -F xxxxxxxxxxxxxxxxxx
$ fgrep xxxxxxxxxxxxxxxxxxxxxx