在文件中查找文本常用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
本文介绍如何使用grep命令进行文本搜索,包括基本语法、常见选项如-v、-n、-c、-e以及示例应用,帮助读者掌握高效文本搜索技巧。

被折叠的 条评论
为什么被折叠?



