过滤文件内容 --- grep
# grep [option] "pattern" 文件名称
pattern模式:
由普通字符和正则表达式的元字符组成的条件
[root@node01 ~]# grep "root" /etc/passwd
正则表达式的元字符
1) 匹配单个字符的元字符
.任意单个字符
[root@node01 ~]# grep "r..t" /etc/passwd
[abc]或者
[root@node01 ~]# grep "r[aA]t" /tmp/1.txt
-连续的字符范围
[a-z][A-Z][a-zA-Z][0-9][a-zA-Z0-9]
[root@node01 ~]# grep "r[a-z]t" /tmp/1.txt
[root@node01 ~]# grep "r[0-9]t" /tmp/1.txt
[root@node01 ~]# grep "r[a-zA-Z0-9]t" /tmp/1.txt
^取反
[^a-z]
[root@node01 ~]# grep "r[^0-9]t" /tmp/1.txt
特殊的字符集:
[[:punct:]]任意单个标点
[[:space:]]任意单个空白字符
[root@node01 ~]# grep "r[[:punct:]]t" /tmp/1.txt
[root@node01 ~]# grep "r[[:space:]]t" /tmp/1.txt
2) 匹配字符出现的位置
^string 以string开头
[root@node01 ~]# grep "^root" /etc/passwd
[root@node01 ~]# grep "^[rbh]" /etc/passwd
[root@node01 ~]# grep "^[^rbh]" /etc/passwd
string$以string结尾
[root@node01 ~]# grep "bash$" /etc/passwd
[root@node01 ~]# grep "nologin$" /etc/passwd | wc -l
^$空行
[root@node01 ~]# grep "^$" /etc/fstab | wc -l
[root@node01 ~]# ls -l /etc/ | grep "^d"
3) 匹配字符出现的次数
*匹配其前一个字符出现任意次
.*任意字符
[root@node01 ~]# grep "ab*" /tmp/2.txt
\?0次或者1次 可有可无
[root@node01 ~]# grep "ab\?" /tmp/2.txt
\+1次或者多次最少1次
[root@node01 ~]# grep "ab\+" /tmp/2.txt
\{2\}精确匹配2次
[root@node01 ~]# grep "ab\{2\}" /tmp/2.txt
\{2,5\}最少2次,最多5次
[root@node01 ~]# grep "ab\{2,5\}" /tmp/2.txt
\{2,\}最少2次
[root@node01 ~]# grep "ab\{2,\}" /tmp/2.txt
分组\(ab\)\+
[root@node01 ~]# grep "\(ab\)\{2,\}" /usr/share/dict/words
option选项:
1) -i 忽略大小写
[root@node01 ~]# grep -i "^r" /tmp/1.txt
2) -o 仅显示符合正则表达式的内容, 不再显示整行
[root@node01 ~]# grep -o "r..t" /etc/passwd
3) -v反向过滤
[root@node01 ~]# grep -v "^#" /etc/fstab
4) -e 根据多个条件过滤文本
[root@node01 ~]# grep -e "^$" -e "^#" /etc/fstab
[root@node01 ~]# grep -v -e "^$" -e "^#" /etc/fstab
5) -E支持扩展正则表达式
[root@node01 ~]# grep -E "(ab){2,}" /usr/share/dict/words
[root@localhost ~]# grep -E "vmx|svm" /proc/cpuinfo
6) -A n 同时显示符合条件后n行
[root@node01 ~]# ifconfig eth0 | grep -A 2 "netmask"
7) -B n 同时显示符合条件前n行
[root@node01 ~]# ifconfig eth0 | grep -B 2 "netmask"
[[:pace:]]任意单个空白字符
[root@node01 ~]# grep "r[[:punct:]]t" /tmp/1.txt
[root@node01 ~]# grep "r[[:space:]]t" /tmp/1.txt
2) 匹配字符出现的位置
^string 以string开头
[root@node01 ~]# grep "^root" /etc/passwd
[root@node01 ~]# grep "^[rbh]" /etc/passwd
[root@node01 ~]# grep "^[^rbh]" /etc/passwd
string$以string结尾
[root@node01 ~]# grep "bash$" /etc/passwd
[root@node01 ~]# grep "nologin$" /etc/passwd | wc -l
^$空行
[root@node01 ~]# grep "^$" /etc/fstab | wc -l
[root@node01 ~]# ls -l /etc/ | grep "^d"
3) 匹配字符出现的次数
*匹配其前一个字符出现任意次
.*任意字符
[root@node01 ~]# grep "ab*" /tmp/2.txt
\?0次或者1次 可有可无
[root@node01 ~]# grep "ab\?" /tmp/2.txt
\+1次或者多次最少1次
[root@node01 ~]# grep "ab\+" /tmp/2.txt
\{2\}精确匹配2次
[root@node01 ~]# grep "ab\{2\}" /tmp/2.txt
\{2,5\}最少2次,最多5次
[root@node01 ~]# grep "ab\{2,5\}" /tmp/2.txt
\{2,\}最少2次
[root@node01 ~]# grep "ab\{2,\}" /tmp/2.txt
分组\(ab\)\+
[root@node01 ~]# grep "\(ab\)\{2,\}" /usr/share/dict/words
option选项:
1) -i 忽略大小写
[root@node01 ~]# grep -i "^r" /tmp/1.txt
2) -o 仅显示符合正则表达式的内容, 不再显示整行
[root@node01 ~]# grep -o "r..t" /etc/passwd
3) -v反向过滤
[root@node01 ~]# grep -v "^#" /etc/fstab
4) -e 根据多个条件过滤文本
[root@node01 ~]# grep -e "^$" -e "^#" /etc/fstab
[root@node01 ~]# grep -v -e "^$" -e "^#" /etc/fstab
5) -E支持扩展正则表达式
[root@node01 ~]# grep -E "(ab){2,}" /usr/share/dict/words
[root@localhost ~]# grep -E "vmx|svm" /proc/cpuinfo
6) -A n 同时显示符合条件后n行
[root@node01 ~]# ifconfig eth0 | grep -A 2 "netmask"
7) -B n 同时显示符合条件前n行
[root@node01 ~]# ifconfig eth0 | grep -B 2 "netmask"
转载于:https://blog.51cto.com/lyw168/1957345