Linux文本查找:grep

本文介绍如何使用grep命令进行文本搜索,包括基本语法、常见选项如-v、-n、-c、-e以及示例应用,帮助读者掌握高效文本搜索技巧。

在文件中查找文本常用grep,格式:
grep [选项] 筛选字段 文件名

简单示例:

shaphicprb13137:/home # cat test
1
190
2
3
58
210
21
20
205
2056
adf
wed
cd
s
cva
ghr

shaphicprb13137:/home # grep 1 test 		//在test文件中查找含有1的行
1
190
210
21

常用选项
-v:反向查找;
-n:显示行号
-c:显示匹配的行数;
-e:多个匹配模式时,用-e来指定每个模式
示例:

shaphicprb13137:/home # grep 1 test
1
190
210
21
shaphicprb13137:/home # grep -c 1 test
4
You have new mail in /var/mail/root
shaphicprb13137:/home # grep -n 1 test
1:1
2:190
6:210
7:21
shaphicprb13137:/home # grep -v 1 test
2
3
58
20
205
2056
adf
wed
cd
s
cva
ghr

shaphicprb13137:/home # grep -e 2 -e 6 test			//-e的选项是或的关系,等同于[模式1模式2]
2
210
21
20
205
2056
shaphicprb13137:/home # grep [26] test		//[]中是或的关系,将要筛选的字符放进去,不需要加逗号
2
210
21
20
205
2056
You have new mail in /var/mail/root

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值