grep过滤命令应用

grep命令是查找文本内容命令


 1.   -v,取反匹配
 2.   -i,忽略大小
 3. ^..,开头匹配
 4.  ...$,结尾匹配
 5.  ^$,匹配空行

1)查找包含root选项

[root@room4pc09 桌面]# grep 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

2)查找不含root文字选项

[root@room4pc09 桌面]# grep -v 'root' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin

3)查找忽略大小写 root 文字选择

[root@room4pc09 桌面]# grep -i 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

4)查找必须与 root 开头文字选择 ^

[root@room4pc09 桌面]# grep '^root' /etc/passwd
root:x:0:0:root:/root:/bin/bash

5)查找必须 bash 结尾文字选择$

[root@room4pc09 桌面]# grep 'bash$' /etc/passwd
root:x:0:0:root:/root:/bin/bash
Student:x:1000:1000::/home/student:/bin/bash
mysql:x:1009:1009::/home/mysql:/bin/bash
dt:x:1609:1609::/home/dt:/bin/bash
fei:x:1000:1000::/home/fei:/bin/bash
pan1:x:1610:1617::/home/pan1:/bin/bash
pan2:x:1611:1611::/home/pan2:/bin/bash
pan3:x:1612:1612::/home/pan3:/bin/bash
pan4:x:1613:1618::/home/pan4:/bin/bash

6)查找不含空行,不含#开头 文件

[root@room4pc09 ~]# grep -v '^$' /etc/login.defs | grep -v '^#' 
MAIL_DIR	/var/spool/mail
PASS_MAX_DAYS	99999
PASS_MIN_DAYS	0
PASS_MIN_LEN	5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值