Linux 命令之grep

简介: 案例1:精准匹配(-w)[root@localhost test]# cat 1.txt | grep root 1.txt --color=auto1-1.png[root@localhost test]# cat 1.txt | grep -w "root" 1.txt --color=auto1-2.png注: 可以看出来,加-w参数会精准匹配要匹配的单词,并且是区分呢大小写匹配。
案例1:精准匹配(-w)
[root@localhost test]# cat 1.txt | grep root 1.txt --color=auto
img_aa335870914fe1e1f7c2402c17d86269.png
1-1.png
[root@localhost test]# cat 1.txt | grep -w "root" 1.txt --color=auto
img_01c140c953d03afbf59ddf9f3081f3c8.png
1-2.png

注: 可以看出来,加-w参数会精准匹配要匹配的单词,并且是区分呢大小写匹配。其中 参数--color=auto 是加入自动颜色,就是我们匹配的单词高亮显示

案例2:取反参数(-v)
[root@localhost test]# ps -ef | grep ssh
img_345fdbf511694740f700906a3a51e077.png
2-1.png
[root@localhost test]# ps -ef | grep ssh | grep -v grep
img_0fb911ffbbde2489b76aa21b6666b169.png
2-2.png
案例3:统计出现的行数数量(-c)
[root@localhost test]# grep -c "root" 1.txt
img_b3553d42a47a13ef33d0cf351273049c.png
3-1.png
案例4:显示匹配的行数(-n)
[root@localhost test]# grep -n "root" 1.txt --color=auto
img_87af73ff4c3f48b1d1bec7b98c1fbc15.png
4-1.png
案例5:显示匹配的文件(-l)
[root@localhost test]# grep "root" 1.txt 2.txt 3.txt
img_4a6413ed2b0ce513fe6d2fbab69f33a3.png
5-1.png
[root@localhost test]# grep -l "root" 1.txt 2.txt 3.txt
img_fd1e1e189d33309f3db43c95f7f0d14f.png
5-2.png
案例6:忽略文件大小写(-i)
[root@localhost test]# cat 1.txt | grep -i "root" --color=auto
img_26235215f13f1067068056662de6db91.png
6-1.png
案例7:控制字符范围
[root@localhost test]# seq 10 | grep "5" -A 3
img_a9b89e6db99f17b36f55bb7988761479.png
7-1.png
[root@localhost test]# seq 10 | grep "5" -B 3
img_c64beaf13c795a8d69f0d2a79c63109f.png
7-2.png
[root@localhost test]# seq 10 | grep "5" -C 3
img_7600a73e7aa58c68132f638b3ac22fb6.png
7-3.png

通过以上图示实验,可以明白:
-A n 向下匹配n行
-B n 向上匹配n行
-C n 同时向上向下匹配n行

目录
相关文章
|
23天前
|
Ubuntu Linux
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
查看Linux系统架构的命令,查看linux系统是哪种架构:AMD、ARM、x86、x86_64、pcc 或 查看Ubuntu的版本号
144 3
|
18天前
|
机器学习/深度学习 安全 网络协议
Linux防火墙iptables命令管理入门
本文介绍了关于Linux防火墙iptables命令管理入门的教程,涵盖了iptables的基本概念、语法格式、常用参数、基础查询操作以及链和规则管理等内容。
178 73
|
11天前
|
Linux Shell
Linux 中 Tail 命令的 9 个实用示例
Linux 中 Tail 命令的 9 个实用示例
40 6
Linux 中 Tail 命令的 9 个实用示例
|
7天前
|
设计模式 Java Linux
Linux的20个常用命令
Linux的23个常用命令
Linux的20个常用命令
|
3天前
|
存储 Linux Shell
linux查找技巧: find grep xargs
linux查找技巧: find grep xargs
23 13
|
16天前
|
Linux 应用服务中间件 nginx
|
3天前
|
机器学习/深度学习 存储 Linux
linux中强大且常用命令:find、xargs、grep
linux中强大且常用命令:find、xargs、grep
25 9
|
3天前
|
SQL 移动开发 Linux
linux下find、grep命令详解
linux下find、grep命令详解
38 8
|
10天前
|
存储 Linux 编译器
linux中vim介绍以及常用命令大全
linux中vim介绍以及常用命令大全
32 8
|
12天前
|
机器学习/深度学习 安全 网络协议
Web安全-Linux网络命令
Web安全-Linux网络命令
14 1