首页 > 代码库 > 正则表达式:grep

正则表达式:grep

[root@localhost ~]# grep ‘root‘ /etc/passwd           # 过滤出带有‘root‘的行
[root@localhost ~]# grep ‘[0-9]‘ /etc/passwd # 过滤出带有数字的行
[root@localhost ~]# grep ‘[a-zA-Z]‘ /etc/passwd # 过滤出带有字母的行
[root@localhost ~]# grep ‘^$‘ /etc/passwd # 过滤出空行
[root@localhost ~]# grep ‘^[0-9]‘ /etc/passwd # 过滤出以数字开头的行
[root@localhost ~]# grep ‘[^0-9]‘ /etc/passwd # 过滤出不包含数字的行
[root@localhost ~]# grep ‘^[^0-9]‘ /etc/passwd # 过滤出不以数字开头的行
[root@localhost ~]# grep ‘r.o‘ /etc/passwd # . 表示匹配任意一个字符
[root@localhost ~]# grep ‘r*o‘ /etc/passwd # * 表示匹配任意个前面的字符,包括零个
[root@localhost ~]# grep ‘r\?o‘ /etc/passwd # ? 表示匹配零个或一个前面的字符,注意要使用转义字符
[root@localhost ~]# grep ‘r.*o‘ /etc/passwd # 表示过滤出以‘r‘开头并以‘o‘结尾的行,中间可以是任意字符[root@localhost
~]# grep --color ‘root‘ /etc/passwd # 过滤出带有‘root‘的行并显示颜色[root@localhost ~]# grep -n ‘root‘ /etc/passwd # 过滤出带有‘root‘的行并显示行号[root@localhost ~]# grep -c ‘root‘ /etc/passwd # 只统计出有多少行带有‘root‘关键字[root@localhost ~]# grep -v ‘root‘ /etc/passwd # 过滤出不带有‘root‘的行[root@localhost ~]# grep -A 2 ‘root‘ /etc/passwd # 过滤出带有‘root‘的行并在后面再显示两行[root@localhost ~]# grep -B 2 ‘root‘ /etc/passwd # 过滤出带有‘root‘的行并在前面再显示两行[root@localhost ~]# grep -C 2 ‘root‘ /etc/passwd # 过滤出带有‘root‘的行并在前面和后面再显示两行[root@localhost ~]# grep -rl ‘nginx‘ /usr/local/ # 在一个目录中过滤出带有‘nginx‘的文件

 

 

 

 

    

正则表达式:grep