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行

目录
相关文章
|
11天前
|
Linux 数据安全/隐私保护 Windows
命令方式:window向linux传文件
【10月更文挑战第6天】本文介绍了如何在Linux系统中通过命令`ip a`获取IP地址,并在Windows系统下使用CMD命令行工具和SCP命令实现文件传输。示例展示了如何将D盘中的`mm.jar`文件上传至IP地址为192.168.163.122的Linux系统的/up/目录下,最后在Linux系统中确认文件传输结果。
167 65
|
3天前
|
安全 Linux
Linux系统之lsof命令的基本使用
【10月更文挑战第14天】Linux系统之lsof命令的基本使用
25 2
Linux系统之lsof命令的基本使用
|
11天前
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
36 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
|
13天前
|
监控 安全 网络协议
|
5天前
|
Linux
Linux 系统五种帮助命令的使用
Linux 系统五种帮助命令的使用
31 14
|
1天前
|
运维 网络协议 Linux
linux系统命令 losf详解
**lsof命令**(List Open Files)是Linux系统中一个非常实用的工具,用于列出当前系统上所有打开的文件以及与之关联的进程。以下是对lsof命令的详细介绍: ### 一、基本功能 lsof命令可以显示系统中被进程打开的文件,这些文件可以是普通文件、目录、网络套接字、设备文件等。通过lsof命令,用户可以方便地查看哪些文件被哪些进程打开,以及这些文件的状态信息。 ### 二、基本语法 lsof命令的基本语法为:`lsof [选项] [文件]`。其中,选项用于指定lsof命令的行为,文件则是可选的,用于指定要查询的文件。 ### 三、常用选项 * `-a` 或 `-
|
1天前
|
Linux Perl
Linux awk命令使用技巧
【10月更文挑战第16天】Linux awk命令使用技巧
9 4
|
6天前
|
Linux
Linux经常使用命令汇总和总结
Linux经常使用命令汇总和总结
25 1
|
6天前
|
安全 Linux Shell
Linux | Rsync 命令:16 个实际示例(上)
Linux | Rsync 命令:16 个实际示例(上)
20 0
Linux | Rsync 命令:16 个实际示例(上)
|
9天前
|
存储 安全 Linux
Linux文件管理命令md5sum awk
通过结合 `md5sum`和 `awk`,不仅可以高效地进行文件完整性校验,还能灵活地处理和分析校验结果,为系统管理、数据审计等工作提供强大的支持。
20 2