美文网首页
基础正则学习grep

基础正则学习grep

作者: 蓝山_d851 | 来源:发表于2020-11-02 10:28 被阅读0次

1.下载模板问题

[root@test001 ~]# wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt
[root@test001 ~]# vim regular_express.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!     My god!
The gd software is a library for drafting programs.^M
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird

文件共22行最下面行为空格。我们开始吧

1.搜索指定字符串

[root@test001 tmp]#  grep    'the'   regular_express.txt 
I can't finish the test.
the symbol '*' is represented as start.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
google is the best tools for search keyword.

2.取反数值

[root@test001 tmp]# grep   -vn  "the"  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.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
13:Oh!  My god!
14:The gd software is a library for drafting programs.
17:I like dog.
19:goooooogle yes!
20:go! go! Let's go.
21:# I am VBird
22:

不论大小写取'the'值

[root@test001 tmp]# grep   -vn  "the"  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.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
13:Oh!  My god!
14:The gd software is a library for drafting programs.
17:I like dog.
19:goooooogle yes!
20:go! go! Let's go.
21:# I am VBird
22:

搜索集合

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

不想要oo字段前面有g

[root@test001 tmp]# grep   -n  "t[ae]st"  regular_express.txt 
8:I can't finish the test.
9:Oh! The soup taste good.
[root@test001 tmp]# 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@test001 tmp]# grep   -n  "[^a-z]oo"   regular_express.txt 
3:Football game is not use feet only.

只要有数字的行

[root@test001 tmp]# 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.

取行首“the”的行

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

不想要开头的英文字母

[root@test001 tmp]# grep  "^[^a-zA-Z]"  regular_express.txt 
"Open Source" is a good mechanism to develop programs.
# I am VBird

查找以 " . " 结尾的字段

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

匹配空行

[root@test001 tmp]# grep  -n  "^$"  regular_express.txt 
22:

不匹配空行以及#开头的行

[root@test001 tmp]# grep   -v "^$"   regular_express.txt   |  grep   -v  "^#"   
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh! My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.

任意一个字符“ . ” 与重复字符 “ * ”

[root@test001 tmp]# 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".

查找 g开头与g结尾并且两个g之中最少要有一个o

[root@test001 tmp]# grep  -n   "goo*g"  regular_express.txt 
18:google is the best tools for search keyword.
19:goooooogle yes!

想要找出g开头与g结尾得字符串,当中得字符可有可无

[root@test001 tmp]# grep  -n  "g*g"   regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
9:Oh! The soup taste good.
13:Oh!  My god!
14:The gd software is a library for drafting programs.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

找出g开头与g结尾之间1个或者多个字符

[root@test001 tmp]# grep  -n  "g.*g"   regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

找出任意数字的行列

[root@test001 tmp]# 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.

限定连续RE字符范围{ }
找到两个o的字符串

[root@test001 tmp]# 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!

g后面2-5个o并且结尾是g

[root@test001 tmp]# grep  -n   "go\{2,5\}g"  regular_express.txt 
18:google is the best tools for search keyword.

查找两个o以上的goooo....里

[root@test001 tmp]# grep -n  "go\{2,\}"  regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

相关文章

  • 基础正则学习grep

    1.下载模板问题 文件共22行最下面行为空格。我们开始吧 1.搜索指定字符串 2.取反数值 不论大小写取'the'...

  • grep正则

    91 正则介绍_grep(上) 正则介绍grep grep -c 显示行数 grep -n 显示行号 grep -...

  • 三剑客_grep

    grep grep 相关参数 grep + 正则表达式(扩展正则)

  • 基础-26、正则grep

    笔记内容:9.1 正则介绍_grep上9.2 grep中9.3 grep下扩展把一个目录下,过滤所有.php文档中...

  • 7月23日 正则符号

    系统通配符号 * 匹配所有内容 {} 匹配序列信息 系统正则符号 主要在grep sed awk 命令用基础正则b...

  • 十一、正则表示法与文件格式处理

    1、基础正则表示法的练习 1.1 搜寻特殊字符grep -n 'the' regular_express.txtg...

  • 2018-10-19

    10月16日任务 9.1 正则介绍_grep上 9.2 grep中 9.3 grep下 正则介绍 9.1 _gre...

  • Shell编程

    基础正则表达式 1、正则表达式与通配符 正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配。grep、aw...

  • Linux Day21:grep/sed/awk

    grep 基础正则表达式 下载练习文件wget http://linux.vbird.org/linux_basi...

  • Linux简单学习记录(八)

    正则表达式和Shell脚本 一、正则表达式 1.grep与egrep工具 grep命令的格式grep [-cinv...

网友评论

      本文标题:基础正则学习grep

      本文链接:https://www.haomeiwen.com/subject/xznkvktx.html