软件测试/测试开发|一文详解Linux grep命令

简介: 软件测试/测试开发|一文详解Linux grep命令

image.png

简介

grep命令是最常用的Linux命令之一,用于对文件和文本执行重复搜索的工具,功能非常强大,也是我们必须学习掌握的Linux三剑客之一,本文就给大家介绍一下grep命令的使用。

grep的基本语法

grep作为一款文本搜索工具,可以根据用户指定的“模式(过滤条件)”对目标文本逐行进行匹配检查,打印匹配到的行,基本的语法如下:

grep [options] [pattern] file

命令  参数   匹配模式  文件

常用的参数如下:

  • -i:忽略模式中的字母大小写。
  • -c:仅列出文件中包含模式的行数。
  • -l:列出带有匹配行的文件名。
  • -v:列出没有匹配模式的行。
  • -w: 把表达式当做一个完整的单字符来搜寻,忽略那些部分匹配的行。
  • -o:只输出匹配的内容。
  • -E:使用egrep命令。

grep命令使用实践

首先我们来看一下我们需要操作的文件的内容,命令如下:

cat -n test.txt
     1    I am a coach
     2    I am a player
     3    I like Linux
     4    I like python
     5    hello world
     6    
     7    mia san mia
     8    12312341
     9    321789
    10    abcdrstxyz.
    11    efglmnuvw.
    12    
    13    #tigaffpubg
    14    #theshyrookie
    15    
    16    i believe i can fly
  1. 输出以I 开头的行(不区分大小写)
 grep "^i" test.txt -i -n
1:I am a coach
2:I am a player
3:I like Linux
4:I like python
16:i believe i can fly

注: 这里的-i参数代表不区分大小写, -n参数代表显示匹配行和行号

  1. 输出以.结尾的行
 grep "\.$" test.txt -n
10:abcdrstxyz.
11:efglmnuvw.

注: 因为.在这里有着特殊含义, 所以要用\转义一下, 如果不加转义字符的话, grep就会把它当做正则表达式来处理(.代表的含义是匹配任意一个字符)

  1. $符号

在Linux平台下, 所有文件每行结尾都有一个$符,我们可以使用cat命令进行查看,如下:

$ cat -A test.txt 
I am a coach$
I am a player$
I like Linux$
I like python$
hello world$
$
mia san mia$
12312341$
321789$
abcdrstxyz.$
efglmnuvw.$
$
#tigaffpubg$
#theshyrookie$
$
i believe i can fly$
  1. 查找空行

^$即表示空行,找出空行的命令如下:

$ grep "^$" test.txt -n
6:
12:
15:
  1. 查找有内容的行

.点表示任意一个字符, 有且只有一个, 不包含空行,所以输出有内容的行命令如下:

$ grep "." test.txt -n
1:I am a coach
2:I am a player
3:I like Linux
4:I like python
5:hello world
7:mia san mia
8:12312341
9:321789
10:abcdrstxyz.
11:efglmnuvw.
13:#tigaffpubg
14:#theshyrookie
16:i believe i can fly
  1. "*"符号

*表示找出前一个字符0次或一次以上,命令如下:

$ grep "i*" test.txt -n
1:I am a coach
2:I am a player
3:I like Linux
4:I like python
5:hello world
6:
7:mia san mia
8:12312341
9:321789
10:abcdrstxyz.
11:efglmnuvw.
12:
13:#tigaffpubg
14:#theshyrookie
15:
16:i believe i can fly
  1. .*组合符

.*表示所有内容, 包括空行,示例代码如下:

$ grep ".*" test.txt -n
1:I am a coach
2:I am a player
3:I like Linux
4:I like python
5:hello world
6:
7:mia san mia
8:12312341
9:321789
10:abcdrstxyz.
11:efglmnuvw.
12:
13:#tigaffpubg
14:#theshyrookie
15:
16:i believe i can fly
  1. []中括号

中括号表达式,[abc]表示匹配中括号中任意一个字符, abc,常见的形式如下,

  • [a-z]匹配所有小写单个字母[A-Z]匹配所有单个大写字母
  • [a-zA-Z]匹配所有的单个大小写字母
  • [0-9]匹配所有单个数字
  • [a-zA-ZO-9]匹配所有数字和字母

匹配abc字符中的任意一个,得到它的行数和行号

$ grep "[abc]" test.txt -n
1:I am a coach
2:I am a player
7:mia san mia
10:abcdrstxyz.
13:#tigaffpubg
16:i believe i can fly

注:如果我们想查看不包含abc字符的行,则可以写成[^abc]

  1. -o参数

使用-o参数, 可以值显示被匹配到的关键字, 而不是将整行的内容都输出。命令如下:

 grep "[abc]" test.txt -n -o
1:a
1:a
1:c
1:a
1:c
2:a
2:a
2:a
7:a
7:a
7:a
10:a
10:b
10:c
13:a
13:b
16:b
16:c
16:a

查看一共有多少行,我们可以改用-c参数,命令如下:

$ grep "[abc]" test.txt -c
6

扩展正则表达式grep

grep命令结合正则之后,功能将更为强大,我们这里使用-E进行正则扩展。

  1. +

+号表示匹配前一个字符1一次或多次,必须使用grep -E扩展正则,命令如下:

$ grep -E "i+" test.txt -n
3:I like Linux
4:I like python
7:mia san mia
13:#tigaffpubg
14:#theshyrookie
16:i believe i can fly
  1. ?

匹配前一个字符0次或1次,命令使用如下:

$ grep -E "li?e" test.txt  -n
16:i believe i can fly
  1. {n,m}匹配次数
  • {n,m}:匹配前一个字符至少n次, 最多m次

  • {n,}: 匹配前一个字符至少n次, 没有上限

  • {,m}: 匹配前一个字符最多m次,可以没有

示例命令如下:

$ grep -E "a{1,3}" test.txt 
I am a coach
I am a player
mia san mia
abcdrstxyz.
#tigaffpubg
i believe i can fly

总结

本文主要介绍了Linux三剑客中的grep命令的使用,这是一个功能非常强大的文本内容搜索工具,熟练的使用grep命令,对于我们学好软件测试开发的课程有很大的帮助,希望本文能够帮到大家!

相关文章
|
14天前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
150 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
14天前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
229 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
1月前
|
Unix Linux 程序员
Linux文本搜索工具grep命令使用指南
以上就是对Linux环境下强大工具 `grep` 的基础到进阶功能介绍。它不仅能够执行简单文字查询任务还能够处理复杂文字处理任务,并且支持强大而灵活地正则表达规范来增加查询精度与效率。无论您是程序员、数据分析师还是系统管理员,在日常工作中熟练运用该命令都将极大提升您处理和分析数据效率。
112 16
|
24天前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
381 0
|
24天前
|
Linux
linux命令—stat
`stat` 是 Linux 系统中用于查看文件或文件系统详细状态信息的命令。相比 `ls -l`,它提供更全面的信息,包括文件大小、权限、所有者、时间戳(最后访问、修改、状态变更时间)、inode 号、设备信息等。其常用选项包括 `-f` 查看文件系统状态、`-t` 以简洁格式输出、`-L` 跟踪符号链接,以及 `-c` 或 `--format` 自定义输出格式。通过这些选项,用户可以灵活获取所需信息,适用于系统调试、权限检查、磁盘管理等场景。
|
2月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
131 18
|
16天前
|
存储 安全 Linux
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
248 0
|
安全 Linux 测试技术
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
106 0
|
1月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
105 0
|
2月前
|
Linux 网络安全 开发工具
技术栈:这50条最常用的 Linux 命令你一定要会!
建议多在终端中实践,遇到不懂的命令就用 man 或 --help 了解详情!
418 0