本文共 4917 字,大约阅读时间需要 16 分钟。
Grep是一种文本过滤工具, 全称是Global search regular expression and print out the line, 其工作方式是根据用户给定的模式(由正则表达式字符及文本字符所编写的过滤条件)对目标文件逐行进行匹配检查, 默认打印匹配到的行
Regular Expression是一类由特殊字符及文本字符所编写的模式(Pattern), 其中的一些字符并不表示字符本身的意义, 而表示控制或者通配功能.
Grep支持的正则表达式有两种:# 用法: grep [options] pattern [file]# 此博客只介绍一些常用选项, 更多选项请查看man文档# --color=auto: 对匹配的文本用颜色显示, CentOS 7已默认在别名中加入[root@zabbix ~]# aliasalias grep='grep --color=auto'# -v: 显示不能够被pattern匹配到的行[root@zabbix ~]# grep Kernel /etc/issue Kernel \r on an \m[root@zabbix ~]# grep -v Kernel /etc/issue\S# -i: 匹配时忽略字符大小写[root@zabbix ~]# grep -i ROOT /etc/passwdroot:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin# -o: 仅显示匹配到的字符串[root@zabbix ~]# grep -o root /etc/passwdrootrootrootroot# -q: 静默模式, 不输出任何信息, 可用echo $?查看执行结果[root@zabbix ~]# grep -q root /etc/passwd [root@zabbix ~]# echo $?0# -A num: 显示匹配到的行及行后面的num行[root@zabbix ~]# grep -A 2 user1 /etc/passwduser1:x:1000:1000::/home/user1:/bin/bashuser2:x:2000:2000::/home/user2:/bin/bashuser3:x:2001:2001::/home/user3:/bin/bash# -B num: 显示匹配到的行及行前面的num行[root@zabbix ~]# grep -B 2 user1 /etc/passwd chrony:x:996:994::/var/lib/chrony:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinuser1:x:1000:1000::/home/user1:/bin/bash# -C num: 显示匹配到的行及前后的num行[root@zabbix ~]# grep -C 2 user1 /etc/passwd chrony:x:996:994::/var/lib/chrony:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinuser1:x:1000:1000::/home/user1:/bin/bashuser2:x:2000:2000::/home/user2:/bin/bashuser3:x:2001:2001::/home/user3:/bin/bash# -E: 使用扩展的正则表达式
[root@zabbix ~]# grep -o r..t /etc/passwd rootrootrootrootr/ft
[root@zabbix ~]# grep -o [a-z][a-z][a-z][0-9] /etc/passwdser1ser1ser2... # 此处省略[root@zabbix ~]# grep -o [[:lower:]][[:lower:]][[:lower:]] /etc/passwdbinbinbinbin... # 此处省略
[root@zabbix ~]# grep -o o*t /etc/passwdootootoott...
[root@zabbix ~]# grep -o r.*t /etc/passwdroot:x:0:0:root:/rootrator:x:11:0:operator:/rootr:/var/ftr/lib/avahi-aut...
[root@zabbix ~]# grep -o 'r[[:lower:]]\?t' /etc/passwdratratrat
[root@zabbix ~]# grep -o 'r[[:lower:]]\+t' /etc/passwd rootrootrootratratrootrat
[root@zabbix ~]# grep -o 'u[[:lower:]]\{3\}[[:digit:]]' /etc/passwduser1user1user2...
[root@zabbix ~]# grep -o 'u.\{2,3\}[[:lower:]]' /etc/passwd utdowutdowutdowusr/g...[root@zabbix ~]# grep -o 'u.\{2,\}[[:lower:]]' /etc/passwd utdown:x:6:0:shutdown:/sbin:/sbin/shutdownusr/games:/sbin/nologinutoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologinus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologinus:x:81:81:System message bus:/:/sbin/nologinunt used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologinuser1:x:1000:1000::/home/user1:/bin/bash...
Note: 默认情况下, 正则表达式工作于贪婪模式, 会尽可能的多的去匹配模式
[root@zabbix ~]# grep ^root /etc/passwdroot:x:0:0:root:/root:/bin/bash
[root@zabbix ~]# grep bash$ /etc/passwdroot:x:0:0:root:/root:/bin/bashuser1:x:1000:1000::/home/user1:/bin/bashuser2:x:2000:2000::/home/user2:/bin/bash...
[root@zabbix ~]# grep '^r.\+[[:lower:]]$' /etc/passwdroot:x:0:0:root:/root:/bin/bash
[root@zabbix ~]# grep '\broot' /etc/passwdroot:x:0:0:root:/root:/bin/bash
[root@zabbix ~]# grep 'bash\b' /etc/passwd root:x:0:0:root:/root:/bin/bashuser1:x:1000:1000::/home/user1:/bin/bashuser2:x:2000:2000::/home/user2:/bin/bashuser3:x:2001:2001::/home/user3:/bin/bash...
[root@zabbix ~]# grep '\broot\b' /etc/passwd root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin
Note: 分组括号中的模式匹配到的内容会被正则表达式引擎记录在内部的变量中, 这些变量的命名方式为\1, \2, \3, ...
[root@zabbix ~]# grep '\([[:lower:]]\{1\}\)\1' /etc/passwd root:x:0:0:root:/root:/bin/bashlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologintss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologinapache:x:48:48:Apache:/usr/share/httpd:/sbin/nologinzabbix:x:994:989:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin[root@zabbix ~]# grep -o '\([[:lower:]]\{1\}\)\1' /etc/passwd oooooooooooosssscclloossssttbbbbbb
转载于:https://blog.51cto.com/13501622/2044620