正则表达式就是处理字符串的方法,可以执行查找,删除,替换等特定字符处理程序。
常用命令 grep ,awk ,sed
[: alunm:] 代表英文大小写字符及数字 即0-9 A-Z a-z
[: alpha:] 代表任何英文大小写字符 A-Z a-z
[:blank :] 代表空格键与[Tab]按键
[: dight :] 代表0-9
[: lower:] 代表小写字符 及a-z
grep 常用日常用法
-i 忽略大小写
-n 输出查找的行号
-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.^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
~
~
1.查找特定字符串
[root@localhost tmp]# grep -n 'the' regular_express.txt
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.
忽略大小写 +i 反向取+v
2.利用[ ] 来查找集合字符
列如果想要查找 test 和 taste
[root@localhost tmp]# grep -n 't[ae]st' regular_express.txt
8:I can't finish the test.
9:Oh! The soup taste good.
3.找出oo前面不要小写字符
[root@localhost tmp]# grep -b '[^a-z]oo' regular_express.txt
82:Football game is not use feet only.
4.找出以the开头的行
[root@localhost tmp]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.
5找出以 . 结尾的行
[root@localhost tmp]# 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.
此时的.需要\来转义
6.查找空白行
[root@localhost tmp]# grep -n '^$' regular_express.txt
22:
7.在正则表达式中
. (小数点) 代表一定有一个任意字符的意思
* (星号) 代表重复前一个0到无穷多次的意思,为组合形态
列如 找出g??d 字符串
[root@localhost 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".
8.查找两个o以上的字符串
扫描二维码关注公众号,回复:
3444803 查看本文章
[root@localhost tmp]# grep -n 'ooo*' 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!
9.查找g……g 的字符串
[root@localhost tmp]# grep -n 'g.*' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
3:Football game is not use feet only.
13:Oh! My god!
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.
限制一个范围区间匹配重复字符数
10.假设找到连续ooo 的字符串
[root@localhost 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!
11.匹配3-5个oo
[root@localhost tmp]# grep -n 'o\{3,5\}' regular_express.txt
19:goooooogle yes!