linux中grep用法详解

查找特定字符串并颜色显示
[root@fwq test]# grep -n 'the' regular_express.txt --color=auto
8:I can't finish the test.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
18:google is the best tools for search keyword.

 

反向选择查找特定字符串并颜色显示

[root@fwq test]# grep -vn 'the' regular_express.txt --color=auto
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
......

忽略大小写查找特定的字符

 [root@fwq test]#  grep -in 'the' regular_express.txt

使用 [] 来查找集合字符:

[root@fwq test]# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.

查找有‘oo’字符串

[root@fwq test]# grep -n 'oo' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

查找有‘oo’字符串,但是不要前面有g的,即剔除goo

[root@fwq test]# grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!

查找非小写字母加oo的内容

[root@fwq test]# grep -n '[^a-z]oo' regular_express.txt
3:Football game is not use feet only.


 

获取有数字的一行

[root@fwq test]# grep -n '[0-9]' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.

[root@fwq test]# grep -n '[^[:lower:]]oo' regular_express.txt
3:Football game is not use feet only.

 

查询以the开头的字符

[root@fwq test]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.

查询开头是小写字母的内容

[root@fwq test]# grep -n '^[a-z]' regular_express.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

查询第一个字符不是大写的

[root@fwq test]# grep -n '^[[:lower:]]' regular_express.txt
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

查询不是以英文开头的字符

[root@fwq test]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird

查询结尾是小数点的行的内容.

[root@fwq test]# grep -n '\.$' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
11:This window is clear.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
20:go! go! Let's go.

查找“空白行”

[root@fwq test]# grep -n '^$' regular_express.txt
22:

通配符.和*的使用,.(小数点)代表一定有一个任意字符,*代表重复前一个到无穷多次的意思

[root@fwq test]# grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".

查询出现任意数字的行列

[root@fwq test]# grep -n '[0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.

 

查询出现两个o的字符串

[root@fwq test]# grep -n 'o\{2\}' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

查询出现2-5个o的字符串,后面在接一个g的字符串

[root@fwq test]# grep -n 'o\{2,5\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

查询出现2个以上o的字符串,后面在接一个g的字符串

[root@fwq test]# grep -n 'o\{2,\}g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!


 

 

 

 

 

 

 

 

 

 

 


 


 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: grep命令是Linux系统常用的文本搜索工具,它可以在文件查找指定的字符串,并将包含该字符串的行输出到屏幕上。grep命令的使用非常灵活,可以通过各种选项和正则表达式来实现不同的搜索需求。 grep命令的基本语法为: grep [选项] 字符串 文件名 其,选项可以是: -c:只输出匹配行的数量,不输出匹配的内容。 -i:忽略大小写。 -n:输出匹配行的行号。 -v:反向匹配,输出不包含指定字符串的行。 -r:递归搜索指定目录下的所有文件。 常用的正则表达式符号包括: ^:匹配行首。 $:匹配行尾。 .:匹配任意一个字符。 *:匹配前面的字符出现次或多次。 +:匹配前面的字符出现1次或多次。 []:匹配方括号的任意一个字符。 {}:匹配花括号指定的次数。 ():将括号的表达式作为一个整体。 使用grep命令可以方便地查找文件的内容,是Linux系统必备的工具之一。 ### 回答2: grep命令是Linux常用的一种文本搜索工具,它可以在文本文件匹配指定的字符串,并将匹配成功的行输出到终端上。grep命令参数非常多,理解它的使用方法可以提高我们在文本操作的效率。下面详细介绍grep命令的使用。 1. grep基本语法 grep命令的基本语法为: grep [option] pattern [file...] 其,[]表示可选参数,pattern表示匹配的字符串,file表示要搜索的文件名。 2. grep的常用选项 其grep的常用选项有以下几种: -i:匹配字符时忽略大小写 -v:显示不匹配的行 -n:显示匹配的行号 -r:递归搜索子目录下文件 3. grep的使用示例 假设当前目录下有一个文件test.txt,内容为: hello world grep command 下面我们将通过例子来学习grep的使用。 3.1 grep命令的基本使用 用法grep [string] [file] 例如:grep hello test.txt 该命令会在文件test.txt搜索“hello”字符串,并将匹配到的行输出到终端上。 3.2 grep命令不区分大小写 用法grep -i [string] [file] 例如:grep -i HELLO test.txt 该命令会在文件test.txt搜索“hello”字符串,并将匹配到的行输出到终端上,由于-i选项的存在,该命令不区分大小写。 3.3 grep命令显示行号 用法grep -n [string] [file] 例如:grep -n hello test.txt 该命令会在文件test.txt搜索“hello”字符串,并将匹配到的行及其行号输出到终端上。 3.4 grep命令显示不匹配的行 用法grep -v [string] [file] 例如:grep -v hello test.txt 该命令会在文件test.txt搜索不包含“hello”字符串的行,并将匹配到的行输出到终端上。 3.5 grep命令递归搜索子目录下文件 用法grep -r [string] [directory] 例如:grep -r hello . 该命令会在当前目录及子目录下所有包含“hello”字符串的文件搜索,并将匹配到的行输出到终端上。 总结: grep命令是一个非常实用的文本搜索工具,我们可以利用它来高效地搜索并处理我们需要的数据。上述仅是grep命令的基本语法和常用选项,想要更为深入地了解该命令,需要不断地在实践积累和学习。 ### 回答3: grep命令是Linux系统非常常用的命令之一,它的用途是搜索指定文本文件的指定字符串,并将包含该字符串的行打印出来。其用法非常简单,只需要在终端输入以下命令: grep "搜索字符串" 文件名 当然,在实际,我们通常使用的grep命令远不止这一个简单的用法。下面详细介绍grep命令的各种用法。 1.基本用法 grep命令最常用的用法就是查找文件的指定字符串,例如: grep "hello" file.txt 这个命令将会在file.txt文件搜索包含“hello”的行,并将所有找到的行打印出来。 2.指定多个文件 如果要在多个文件查找相同的字符串,可以使用通配符*来指定多个文件,例如: grep "hello" *.txt 这个命令将会搜索所有以.txt结尾的文件包含“hello”的行,并将所有找到的行打印出来。 3.忽略大小写 如果不关心字符串的大小写,可以使用-i参数来忽略大小写,例如: grep -i "hello" file.txt 这个命令将会在file.txt文件搜索包含“hello”或“Hello”或“HELLO”等字符串的行,并将所有找到的行打印出来。 4.递归搜索 如果要在一个目录及其子目录搜索指定字符串,可以使用-r参数来递归搜索,例如: grep -r "hello" /path/to/dir 这个命令将会在/path/to/dir目录及其所有子目录搜索包含“hello”的行,并将所有找到的行打印出来。 5.显示行号 如果要在文件显示包含指定字符串的行号,可以使用-n参数,例如: grep -n "hello" file.txt 这个命令将会在file.txt文件搜索包含“hello”的行,并将所有找到的行以及行号打印出来。 6.显示匹配部分 如果要在文件显示匹配指定字符串的部分,可以使用-o参数,例如: grep -o "hello" file.txt 这个命令将会在file.txt文件搜索包含“hello”的行,并将所有找到的“hello”单词打印出来。 7.反向查找 如果要查找不包含指定字符串的行,可以使用-v参数,例如: grep -v "hello" file.txt 这个命令将会在file.txt文件搜索不包含“hello”的行,并将所有找到的行打印出来。 总之,grep命令非常灵活,可以根据不同的需求进行各种细致精准的搜索。不过需要注意的是,尤其在使用递归搜索的时候,要确保指定目录下的文件确实有读权限,否则grep命令可能会无法工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值