文章目录
- linux平台有最常用的三大文本处理工具:awk/sed/grep grep的功能:搜索指定文件的内容,按照指定的模式匹配,并输出匹配内容所在的行。 需要注意的地方:grep只支持匹配但不能替换匹配到的内容 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest 对应的源码可以访问这里获取: https://github.com/liuhongdi/ 说明:作者:刘宏缔 邮箱: 371125307@qq.com
- [root@blog ~]# whereis grep grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz /usr/share/man/man1p/grep.1p.gz /usr/share/info/grep.info.gz [root@blog ~]# rpm -qf /usr/bin/grep grep-3.1-6.el8.x86_64 如果系统提示找不到grep命令或误删除了命令, 可以用dnf安装 [root@blog ~]# dnf install grep
- 1,查看版本 [root@blog ~]# grep –version grep (GNU grep) 3.1 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>. 2,查看帮助: [root@blog ~]# grep –help 3,查看手册: [root@blog ~]# man grep
- 1,列出所有的nginx进程 #-v:反向匹配,不显示包含指定字串的行 [root@blog ~]# ps auxfww | grep ‘nginx: ‘ | grep -v grep root 14470 0.0 0.0 71028 3340 ? Ss May17 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx nginx 14471 0.0 0.0 102764 7796 ? S May17 0:00 \_ nginx: worker process nginx 14472 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14473 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14474 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14475 0.0 0.0 102436 5992 ? S May17 0:00 \_ nginx: worker process nginx 14476 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14477 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14478 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process 2,列出所有登录成功/失败的记录 #-i: 忽略大小写 [root@blog log]# grep -i “accepted password” /var/log/secure 3,列出所有不是root登录的记录 [root@blog log]# last | grep -v root 4,输出文件名和行号 #-n: 输出匹配行在文件中的行号 [root@blog log]# grep -i -n “accepted password” * 说明:如果有多个文件匹配时,文件名也会显示出来 5,递归查询 # -R :递归查询目录下的子目录及文件 [root@blog nginxlogs]$ grep -R -i iphone * 如果不加 R,默认遇到目录不会继续查询 6,得到匹配到的记录数量 #-c:显示总共有多少行被匹配到,而不显示被匹配到的内容 [root@blog nginxlogs]$ grep -i -c iphone i_ssl.access.log 14811 7,只显示被匹配到的字符串,而不是匹配到的行 #-o:只显示被模式匹配的字符串 [root@blog nginxlogs]$ grep -i -o iphone i_ssl.access.log 8,只匹配单词: #-w:被匹配的文本只能是单词,不能是单词中的一部分 [root@blog nginxlogs]$ grep -i -w advertise i_ssl.access.log 可以匹配: /home/index?advertise=b&now_page_id=0&app_key=iPhone 不能匹配: /advertisement/getList?uid=12345 9,多行显示:显示匹配到的行及其后指定数量的行 匹配accepted的行及其前10行 [root@blog log]# grep -B 10 -i “accepted” secure 匹配accepted的行及其后10行 [root@blog log]# grep -A 10 -i “accepted” secure 匹配accepted的行及其前后各10行 [root@blog log]# grep -C 10 -i “accepted” secure 10,只显示有匹配行的文件名: #-l:列出文件内容符合指定的样式的文件名称 [root@blog log]# grep -i -R -l “accepted” *
- 1,集合: . :任意一个字符。 [abc] :表示匹配一个字符,这个字符必须是abc中的一个。 [a-zA-Z] :表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。 [^123] :匹配一个字符,这个字符是除了1、2、3以外的所有字符 查询no和od中间是一个小写字母的账号 [root@blog log]# grep “no[a-z]od” /etc/passwd 2,开头和结尾: 查询以bash结尾的账号 [root@blog log]# grep ‘bash$’ /etc/passwd 查询所有以非bash结尾的账号 [root@blog log]# grep ‘[^bash]$’ /etc/passwd 查询以r打头的账号: [root@blog log]# grep ‘^r’ /etc/passwd 查询所有不是r打头的账号 [root@blog log]# grep ‘^[^r]’ /etc/passwd 3,出现次数 x{m} 重复字符x,m次,例子:’a{5}’匹配包含5个a的行。 x{m,} 重复字符x,至少m次,例子:’b{5,}’匹配至少有5个b的行。 x{m,n}重复字符x,至少m次,不多于n次,例子:’c{5,10}’匹配5–10个c的行 passwd 中o出现2次 [root@blog log]# grep “o{2}” /etc/passwd passwd 中o出现最少1次最多2次 [root@blog log]# grep “o{1,2}” /etc/passwd passwd 中o出现最少2次最多不限次 [root@blog log]# grep “o{2,}” /etc/passwd 4,其他例子: 显示一个目录下的所有目录: [root@blog log]# ll -d */ 或 [root@blog log]# ls -l |grep “^d” 列出一个目录下所有非目录的文件 [root@blog log]# ls -l |grep “^[^d]” 列出一个目录下,group和other有权读取的文件 [root@blog log]# ls -l |grep “^-…r..r..”
- [root@blog ~]$ cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)
linux平台有最常用的三大文本处理工具:awk/sed/grep
grep的功能:搜索指定文件的内容,按照指定的模式匹配,并输出匹配内容所在的行。
需要注意的地方:grep只支持匹配但不能替换匹配到的内容
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
[root@blog ~]# whereis grep
grep: /usr/bin/grep /usr/share/man/man1/grep.1.gz /usr/share/man/man1p/grep.1p.gz /usr/share/info/grep.info.gz
[root@blog ~]# rpm -qf /usr/bin/grep
grep-3.1-6.el8.x86_64
如果系统提示找不到grep命令或误删除了命令,
可以用dnf安装
[root@blog ~]# dnf install grep
1,查看版本
[root@blog ~]# grep --version grep (GNU grep) 3.1 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.
2,查看帮助:
[root@blog ~]# grep --help
3,查看手册:
[root@blog ~]# man grep
1,列出所有的nginx进程
#-v:反向匹配,不显示包含指定字串的行
[root@blog ~]# ps auxfww | grep 'nginx: ' | grep -v grep root 14470 0.0 0.0 71028 3340 ? Ss May17 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx nginx 14471 0.0 0.0 102764 7796 ? S May17 0:00 \_ nginx: worker process nginx 14472 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14473 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14474 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14475 0.0 0.0 102436 5992 ? S May17 0:00 \_ nginx: worker process nginx 14476 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14477 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process nginx 14478 0.0 0.0 102436 6048 ? S May17 0:00 \_ nginx: worker process
2,列出所有登录成功/失败的记录
#-i: 忽略大小写
[root@blog log]# grep -i "accepted password" /var/log/secure
3,列出所有不是root登录的记录
[root@blog log]# last | grep -v root
4,输出文件名和行号
#-n: 输出匹配行在文件中的行号
[root@blog log]# grep -i -n "accepted password" *
说明:如果有多个文件匹配时,文件名也会显示出来
5,递归查询
# -R :递归查询目录下的子目录及文件
[root@blog nginxlogs]$ grep -R -i iphone *
如果不加 R,默认遇到目录不会继续查询
6,得到匹配到的记录数量
#-c:显示总共有多少行被匹配到,而不显示被匹配到的内容
[root@blog nginxlogs]$ grep -i -c iphone i_ssl.access.log 14811
7,只显示被匹配到的字符串,而不是匹配到的行
#-o:只显示被模式匹配的字符串
[root@blog nginxlogs]$ grep -i -o iphone i_ssl.access.log
8,只匹配单词:
#-w:被匹配的文本只能是单词,不能是单词中的一部分
[root@blog nginxlogs]$ grep -i -w advertise i_ssl.access.log
可以匹配: /home/index?advertise=b&now_page_id=0&app_key=iPhone
不能匹配: /advertisement/getList?uid=12345
9,多行显示:显示匹配到的行及其后指定数量的行
匹配accepted的行及其前10行
[root@blog log]# grep -B 10 -i "accepted" secure
匹配accepted的行及其后10行
[root@blog log]# grep -A 10 -i "accepted" secure
匹配accepted的行及其前后各10行
[root@blog log]# grep -C 10 -i "accepted" secure
10,只显示有匹配行的文件名:
#-l:列出文件内容符合指定的样式的文件名称
[root@blog log]# grep -i -R -l "accepted" *
1,集合:
. :任意一个字符。
[abc] :表示匹配一个字符,这个字符必须是abc中的一个。
[a-zA-Z] :表示匹配一个字符,这个字符必须是a-z或A-Z这52个字母中的一个。
[^123] :匹配一个字符,这个字符是除了1、2、3以外的所有字符
查询no和od中间是一个小写字母的账号
[root@blog log]# grep "no[a-z]od" /etc/passwd
2,开头和结尾:
查询以bash结尾的账号
[root@blog log]# grep 'bash$' /etc/passwd
查询所有以非bash结尾的账号
[root@blog log]# grep '[^bash]$' /etc/passwd
查询以r打头的账号:
[root@blog log]# grep '^r' /etc/passwd
查询所有不是r打头的账号
[root@blog log]# grep '^[^r]' /etc/passwd
3,出现次数
x{m} 重复字符x,m次,例子:’a{5}’匹配包含5个a的行。
x{m,} 重复字符x,至少m次,例子:’b{5,}’匹配至少有5个b的行。
x{m,n}重复字符x,至少m次,不多于n次,例子:’c{5,10}’匹配5–10个c的行
passwd 中o出现2次
[root@blog log]# grep "o{2}" /etc/passwd
passwd 中o出现最少1次最多2次
[root@blog log]# grep "o{1,2}" /etc/passwd
passwd 中o出现最少2次最多不限次
[root@blog log]# grep "o{2,}" /etc/passwd
4,其他例子:
显示一个目录下的所有目录:
[root@blog log]# ll -d */
或
[root@blog log]# ls -l |grep "^d"
列出一个目录下所有非目录的文件
[root@blog log]# ls -l |grep "^[^d]"
列出一个目录下,group和other有权读取的文件
[root@blog log]# ls -l |grep "^-...r..r.."
发表回复