正则表达式
eg:1 grep 'root' /etc/passwd -------------把有root的行过滤出来 grep --color 'root' /etc/passwd ---------------把过滤出来的高亮显示出来 grep -v 'root' /etc/passwd ------------把不包含root的行显示出来 grep --color -A 2 'root' /etc/passwd --------显示过滤的行,把过滤行后面2行也输出来 grep --color -n -A 2 'root' /etc/passwd -----------有行号 grep --color -r 'iptables' /etc/* --------------------过滤出/etc/下面的文件含iptables grep --color -rh 'iptables' /etc/* -------------不显示所在的文件名 eg:2 grep -n 'root' /etc/passwd ------- grep --color -n 'root' /etc/passwd ---------------- grep --color '[0-9]' /etc/passwd -------------过滤0-9的数字 grep --color '[a-zA-Z]' /etc/passwd grep --color 'r.o' 1.txt grep --color 'r*o' 1.txt ------------查找*号前面的字符 grep -- color 'r.*o' 1.txt ----------------任意一个任意字符,开头是r 结尾是o字符 grep --color 'r?o' 1.txt----------------匹配o .... o 字符 grep --color -E 'r?o' 1.txt -----------跟脱义用法一E grep 3: egrep == grep -E egrep --color 'r+o' 1.txt ---------匹配一个或多个+号前面的字符 . 任意一个字符 * 星号前面字符零个或多个 .* 任意个任意字符 ?0个或1个?前面的字符 + 1或多个1前面的字符 grep --color 'root|nologin' 1.txt ----------匹配root或者nologin grep --color 'root' 1.txt |grep --color 'nologin' ---------匹配2次 egrep --color '(rr)+' 1.txt -----------------匹配整体 grep -E --color '(rr) {1,3} ' 1.txt -----匹配最开始的1-3个r字符 grep -E --color '(rr) {5}' 1.txt ------------匹配5对rr grep -E --color '(rr){5,6}' 1.txt (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |