[shell] grep: una potente herramienta de búsqueda de texto

1. Conocimientos básicos

El comando grep es una poderosa herramienta de búsqueda de texto que usa expresiones regulares para buscar texto y marca las coincidencias en rojo.
El nombre completo de grep es Impresión de expresión regular global, lo que significa la versión de expresión regular global, y su permiso de uso es para todos los usuarios.

Cómo funciona grep:
busca un literal de cadena en uno o más archivos. Los resultados de la búsqueda se envían a la salida estándar sin afectar el contenido del archivo original.

Si desea hacer coincidir varios archivos, separe los nombres de los archivos con espacios, como:grep "match_pattern" file_1 file_2 file_3 ...

 

Parámetros de comando:

 -A<显示行数>:除了显示符合范本样式的那一列之外,并显示该行之后的内容。
 -B<显示行数>:除了显示符合样式的那一行之外,并显示该行之前的内容。
 -C<显示行数>:除了显示符合样式的那一行之外,并显示该行之前后的内容。
 -c:统计匹配的行数
 -e :实现多个选项间的逻辑or 关系
 -E:扩展的正则表达式
 -f FILE:从FILE获取PATTERN匹配
 -F :相当于fgrep
 -i --ignore-case #忽略字符大小写的差别。
 -n:显示匹配的行号
 -o:仅显示匹配到的字符串
 -q: 静默模式,不输出任何信息
 -s:不显示错误信息。
 -v:显示不被pattern 匹配到的行,相当于[^] 反向匹配
 -w :匹配 整个单词

 

2. Ejemplo básico

[root@localhost ~]# grep [-ivnc] '需要匹配的字符' 文件名
#-i不区分大小写
#-c统计包含匹配的行数
#-n输出行号
#-v反向匹配

1. Contiene una cadena

#查找包含name所在行
[root@localhost ~]# grep 'name' tomAndJerry.txt
The cat's name is Tom,what's the mouse's name?
# 忽略大小写
[root@localhost ~]# grep -i 'name' tomAndJerry.txt
The cat's name is Tom,what's the mouse's name?
The mouse's Name is Jerry

2. Encuentra la ubicación de la cadena y el número de filas

#打印出含有name的行所在位置
[root@localhost ~]# grep -n 'name' tomAndJerry.txt
1:The cat's name is Tom,what's the mouse's name?

# c:包含name的行数
[root@localhost ~]# grep -c 'name' tomAndJerry.txt
1
# ci:包含name(不区分大小写)的行数
[root@localhost ~]# grep -ci 'name' tomAndJerry.txt
2

3. Coincidencia inversa

# 打印不包含name的行
[root@localhost ~]# grep -v 'name' tomAndJerry.txt
The mouse's Name is Jerry
They are good friends

# 打印不包含name(不区分大小写)的行
[root@localhost ~]# grep -vi 'name' tomAndJerry.txt
They are good friends

 
 

3. Ejemplo habitual

texto de ejemplo

[root@localhost ~]# cat RegExp.txt
----TEXT BEGIN----

good morning teacher
hello world is a script
gold sunshine looks beautiful
golden time flies
god bless me
what a delicious food
they teast Good
you fell glad
wrong word gooood
wrong word gl0d
wrong word gl2d
wrong word gl3d
www.helloworld.com
www@helloworld@com
Upper case
100% means pure
php have a gd module

-----TEXT END-----

1. Haz coincidir el principio y el final de la línea

#搜索以good开头的行
[root@localhost ~]# grep '^good' RegExp.txt
good morning teacher

coincide con el final de la línea

#搜索以Good结尾的行
[root@localhost ~]# grep 'Good$' RegExp.txt
they teast Good

 

2. Cuenta el número de líneas en blanco

#搜索空行的行数
[root@localhost ~]# grep -c '^$' RegExp.txt
2

 

3. Combina múltiples posibilidades

#搜索包含Good和good的行
[root@localhost ~]# grep '[Gg]ood' RegExp.txt
good morning teacher
they teast Good

 

4. Invertir búsqueda

#搜索一个包含ood的行,但是不能是Good或good
#尖角号表示的是“非”

[root@localhost ~]# grep '[^Gg]ood' RegExp.txt
what a delicious food
wrong word gooood

 

5. Coincide con un número fijo de caracteres arbitrarios

#搜索包含一个词,该词以g开头、紧接着是两个任意字符、再接着是一个d的行
[root@localhost ~]# grep 'g..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

#搜索包含一个词,该词以G或g开头、紧接着是两个任意字符、再接着是一个d的行
[root@localhost ~]# grep '[Gg]..d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

#搜索这样一些行,该行包含某个单词,该词满足如下条件:
#1.第一个字符可以是G或g
#2.第二个字符可以是l或o
#3.第三个字符可以是换行符之外的任意字符
#4.第四个字符一定是d
[root@localhost ~]# grep '[Gg][lo].d' RegExp.txt
good morning teacher
gold sunshine looks beautiful
golden time flies
they teast Good
you fell glad
wrong word gl0d
wrong word gl2d
wrong word gl3d

 

6. Haga coincidir cero con caracteres fijos ilimitados

#搜索这样一些行,该行包含某个单词,该词满足如下条件:
#1.以g开头
#2.g后面跟零到无限个o
#3.零到无限个o后面跟d
[root@localhost ~]# grep go*d RegExp.txt
good morning teacher
god bless me
wrong word gooood
php have a gd module

 

7. Uso de expresiones regulares extendidas egrep

#搜索g和d之间至少有一个o的行
#“+”代表匹配前面的字符1次以上(含1次)
[root@localhost ~]# egrep 'go+d' RegExp.txt
good morning teacher
god bless me
wrong word gooood

#搜索g和d之间只有0个或1个o的行(0次或1次)
#“?”代表匹配前面的字符1次以上
[root@localhost ~]# egrep 'go?d' RegExp.txt
god bless me
php have a gd module

#搜索有glad或gold的行
[root@localhost ~]# egrep 'glad|gold' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad

#搜索有glad或gold的行的另一种写法
[root@localhost ~]# egrep 'g(la|ol)d' RegExp.txt
gold sunshine looks beautiful
golden time flies
you fell glad

 
 
Referencia:
"Guía práctica de comandos del sistema Linux y secuencias de comandos de Shell"

Supongo que te gusta

Origin blog.csdn.net/hiliang521/article/details/131532860
Recomendado
Clasificación