什么是正则表达式
简单的说,正则表达式就是处理字串的方法,他是以行为单位来进行字串的处理行为, 正则表达式通过一些特殊符号的辅助,可以让使用者轻易的达到“搜寻/删除/取代”某特定字串的处理程序!
正则表达式基本上是一种“表达式”, 只要工具程序支持这种表达式,那么该工具程序就可以用来作为正则表达式的字串处理之用。 例如 vi, grep, awk ,sed 等等工具,因为她们有支持正则表达式, 所以,这些工具就可以使用正则表达式的特殊字符来进行字串的处理。但例如 cp, ls 等指令并未支持正则表达式, 所以就只能使用 bash 自己本身的通配符字符而已。
是 Linux 基础当中的基础,虽然也是最难的部分, 不过,如果学成了之后,一定是“大大的有帮助”的!这就好像是金庸小说里面的学武难关:任督二脉! 打通任督二脉之后,武功立刻成倍成长!
关于语系
在英文大小写的编码顺序中,zh_TW.big5 及 C 这两种语系的输出结果分别如下:
LANG=C 时:0 1 2 3 4 ... A B C D ... Z a b c d ...z
LANG=zh_TW 时:0 1 2 3 4 ... a A b B c C d D ... z Z
尤其要记住:
[:alnum] 代表所以的大小写英文字符和数字 0-9 A—Z a-z
[:alpha:] 代表任意英文大小写字符 A-Z a-z
[:lower:] 代表小写字符 a-z
[:upper:] 代表大写字符 A-Z
[:digit:] 代表数字 0-9
练习示例文件
数据来源于鸟哥私房菜
"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.
# I am VBird
进阶 grep
-A n 把匹配成功行之后的n行也同时列出
-B n 把匹配成功行之前的n行也同时列出
范例:
显示
/etc/passwd
含有
[root@e9818e4ea8b3 ~]# grep mail -B 2 -A3 /etc/passwd
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
显示 目标行的前后 3 行
grep mail -C3 /etc/passwd
只显示匹配到的字符
[root@e9818e4ea8b3 ~]# grep -o 'nologin' /etc/passwd
加上统计数量
[root@e9818e4ea8b3 ~]# grep -o -c 'nologin' /etc/passwd
只要文件名
[root@e9818e4ea8b3 ~]# grep -l 'nologin' /etc/passwd
递归查找,就是在一个目录下查找
[root@e9818e4ea8b3 ~]# grep -r 'nologin' /etc/
搜索
test
或taste
[lf@x201t~]$ grep -n 't[ae]st' file1
搜索 oo 但其前面不要有g
[lf@x201t~]$grep -n '[^g]oo' file1
注意:当搜索的行内含有要符合搜索条件时,此行就会忽略明确不要的条件,比如以上的例子就可能会搜索到下面的内容
3:tool is a good tool
8:goooooogle
显示oo 前面非小写字符的行
方法一:
[lf@x201t~]$grep -n ‘[^a-z]oo’ file1
方法二:
[lf@x201t~]$grep -n ‘[^[:lower:]]oo’ file1
显示开否非英文字符的行
[lf@x201t~]$grep -n ‘^[^[:alpha]]’ file1
符号 ^ 在 [] 内时是取反的意思,在 [] 之外是行首的意思
显示行首不是#和;的行
[lf@x201t~]$grep '^[^#;]' file1
找到以 . 结尾的行
[lf@x201t~]$grep -n ‘\.$’ file1
需要用
\
进行转意
查找 开头是 g 和结尾也是 g ,中间的字符可有可无
[lf@x201t~]$grep -n 'g.*g' fiel1
.
代表一个任意字符
*
代表重复零到多个在 其前面的一个字符
.*
代表零个或多个任意字符
查找以a为开头的任意文件名
方法一:
[lf@x201t~]$ls -l a*
方法二:
[lf@x201t~]$ls |grep -n ‘^a.*’
列出 /etc 目录下的链接文件
[lf@x201t~]$ls -l /etc |grep ‘^l’
再统计一下多少个
[lf@x201t~]$ls -l /etc |grep ‘^l’ |wc -l
···
查找仅含有2个o的字符串
[root@www ~]# grep -n 'o{2}' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
19:gooooogle yes!
由于没有限定字符 o 后面的字符,所以会有19行的出现
查找含有2到4个o ,且以 g 结尾的字符串
[root@www ~]# grep -n 'go{2,4}g' regular_express.txt
18:google is the best tools for search keyword.
免费的资源,我们往往不知道去珍惜!点个关注和喜欢吧
作者:xiguatian
链接:https://www.jianshu.com/p/239da12956b6
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。