一,grep的用途:
linux平台有最常用的三大文本處理工具:awk/sed/grep
grep的功能:搜索指定文件的內容,按照指定的模式匹配,並輸出匹配內容所在的行。
需要注意的地方:grep只支持匹配但不能替換匹配到的內容
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,查看grep所屬的rpm包
[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
三,查看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
四,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" *
五,grep中使用正則表達式的例子
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.."
六,查看centos的版本
[root@blog ~]$ cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)