linux中grep -E参数,linux中grep命令

grep [option] "pattern" 文件名

grep "root" /etc/passwd 过滤带有root 字符

正则表达式元字符

1匹配单个字符的元字符

. 表示任意单个字符

[root@localhost ~]# grep "r..t" /etc/passwd

[abc] 或者

[root@localhost ~]# grep "r[aA]t" /tmp/1.txt

- 连续的字符范围

[a-z] [A-Z] [a-zA-Z] [0-9] [a-zA-Z0-9]

[root@localhost ~]# grep "r[A-Z]t" /tmp/1.txt

rAt

[root@localhost ~]# grep "r[0-9]t" /tmp/1.txt

r8t

[root@localhost ~]# grep "r[a-zA-Z0-9]t" /tmp/1.txt

r8t

rAt

^ 取反

[^a-z]

[root@localhost ~]# grep "r[^0-9]t" /tmp/1.txt

rAt

特殊字符集:

[[:punct:]] 任意单个标

[root@localhost ~]# grep -En "r[[:punct:]]t" /tmp/1.txt

11:r,t

12:r?t

13:r"t"

[[:space:]]任意单个空白字符

[root@localhost ~]# grep -En "r[[:space:]]t" /tmp/1.txt

14:r t

2 匹配字符出现的位置

1) ^string 以string 开头

2)^[rbh] 查找以rbh 开头-----取反 ^[^rbh]

[root@localhost ~]# grep "^[^rbh]" /etc/passwd

3) string& 以string 结尾

[root@localhost ~]# grep "bash&" /etc/passwd

[root@localhost ~]# grep "nologin&" /etc/passwd | wc -l

4) ^& 空行

[root@localhost ~]# grep "^&" /etc/fstab | wc -l

显示/etc下的目录名

[root@localhost ~]# ls -l /etc/ | grep "^d"

3 匹配字符出现的次数

* 匹配前一个字符出现的任意次 ab*: a ab abb abbb

[root@localhost ~]# grep "ab*" /tmp/2.txt

.* 任意字符

? 前一个字符最多出现一次 最少出现0次或一次

* 0 or 多次

+ one or more times.(一次或者多次)

{n} 字符精确出现几次

{n,} 最少n次 多了不限

{,m} 最多m次

{n,m} 最少n次 做多m次

\? 0次或者1次 可有可无

[root@localhost ~]# grep "ab\?" /tmp/2.txt

\+ 1次或者多次 最少1次

[root@localhost ~]# grep "ab\+" /tmp/2.txt

\{2\} 精确匹配2次

[root@localhost ~]# grep "ab\{2\}" /tmp/2.txt

\{2,5\} 最少2次 最多5次

[root@localhost ~]# grep "ab\{2,5\}" /tmp/2.txt

\{2,\} 最少2次

[root@localhost ~]# grep "ab\{2,\}" /tmp/2.txt

分组 \(ab)\+

[root@localhost ~]# grep "\(ab\){2,\}" /usr/share/dict/words

option选项:

1) -i 忽略大小写

[root@localhost ~]# grep -i "^r" /tmp/1.txt

2) -o 仅显示符合正则表达式的内容,不再显示整行

xpl@localhost ~]$ grep -o "r..t" /etc/passwd

3) -v 反向过滤

[root@localhost ~]# grep -v "^#" /etc/fstab

4) -e 根据多个选项过滤文本

[root@localhost ~]# grep -e "^&" -e "^#" /etc/fstab

[root@localhost ~]# grep -v -e "^&" -e "^#" /etc/fstab 不显示

5)-E 支持扩展正则表达式

[root@localhost ~]# grep -E "\(ab\){2,\}" /usr/share/dict/words

cpu信息

[root@localhost ~]# cat /proc/cpuinfo

[root@localhost ~]# grep -E "vmx|svm" /proc/cpuinfo

6) -A n 同时显示符合条件的后n行

[root@localhost ~]# ifconfig eth0 | grep -A 2 "network"

-B n 同时显示符合条件前n

[root@localhost ~]# ifconfig eth0 | grep -B 2 "network"

标签:tmp,ab,grep,etc,命令,linux,root,localhost

来源: https://www.cnblogs.com/xpl520/p/11227732.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值