Linux、正则表达与三剑客知识应用实践
1,什么正则表达式:
作用和特殊字符一样,用一个个的字符事先规定好它干什么。
假设;“@”代表“I am” , “!”代表“oldboy”则执行echo “@!”的结果就是输出“I am oldboy”
它是以行为单位处理
2,可以提高效率,快速的获取到想要的内容。
3,适用于三剑客命令 grep , sed ,awk
4,易混淆事项:
1,和通配符区别
2,开发正则,一般是perl兼容正则表达式
3,linux三剑客正则表达式
环境准备:
正则表达式分类:
基本正则表达式 用grep
^$:
root@zhangxiangyu-58 ~]# grep "m$" oldboy.txt
our site is http://www.oldboyedu.com
[root@zhangxiangyu-58 ~]# grep "\!$" oldboy.txt
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
my god ,i am not oldbey,but OLDBOY!
. : 匹配任意一个且只有一个字符,和通配符一样
[root@zhangxiangyu-58 ~]# grep "." oldboy.txt
I am oldboy
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
“.$” 以任意字符结尾
[root@zhangxiangyu-58 ~]# grep ".$" oldboy.txt
I am oldboy
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
\让有意义的字符 脱掉马夹
[root@zhangxiangyu-58 ~]# grep "\.$" oldboy.txt
I teach linux.
my qq num is 49000448.
not 4900000448.
.* 组合符,表示所有
[root@zhangxiangyu-58 ~]# grep ".*" oldboy.txt
I am oldboy
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[abc] 匹配 ,集合[]内的任意一个字符a或b或c,
[root@zhangxiangyu-58 ~]# grep "[abc]" oldboy.txt
I am oldboy
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my god ,i am not oldbey,but OLDBOY!
扩展正则表达式 egrep 或 grep -E
+ 匹配前一个字符1次或1次以上
[root@zhangxiangyu-58 ~]# egrep ".+" oldboy.txt
I am oldboy
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[:/]+ 匹配:或者/ 1次或多次
[root@zhangxiangyu-58 ~]# grep -E "[:/]+" oldboy.txt
our site is http://www.oldboyedu.com
? 匹配前一个字符0次或1次
[root@zhangxiangyu-58 ~]# grep -E "db?" oldboy.txt
I am oldboy
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my god ,i am not oldbey,but OLDBOY!
| 或者 同时过多个字符串
[root@zhangxiangyu-58 ~]# grep -E "oldboy|like" oldboy.txt
I am oldboy
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
[root@zhangxiangyu-58 ~]# grep -E "oldboy|like|000" oldboy.txt
I am oldboy
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
a{n,m} 匹配前一个字符最少n次,最多m次
a{n,} 匹配前一个字符最少n次
a{n} 匹配前一个字符正好n次
a{,m} 匹配前一个字符最多m次
[root@zhangxiangyu-58 ~]# grep -E "0{1,2}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@zhangxiangyu-58 ~]# grep -E "0{5,}" oldboy.txt
not 4900000448.
[root@zhangxiangyu-58 ~]# grep -E "0{5}" oldboy.txt
not 4900000448.
[root@zhangxiangyu-58 ~]# grep -E "0{3}" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@zhangxiangyu-58 ~]# grep -E "0{,3}" oldboy.txt
I am oldboy
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.
not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[root@zhangxiangyu-58 ~]# egrep -o "0{,3}" oldboy.txt
000
000
00
( ) 分组过滤被括起来的东西表示一个整体
[root@zhangxiangyu-58 ~]# egrep "(00)" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@zhangxiangyu-58 ~]# egrep -o "(00)" oldboy.txt
00
00
00
[root@zhangxiangyu-58 ~]# egrep -o "(00)\1" oldboy.txt
0000
[root@zhangxiangyu-58 ~]# egrep -o "00" oldboy.txt
00
00
00
元字符:
\b 匹配单词边界
[root@zhangxiangyu-58 ~]# egrep "\boldboy\b" oldboy.txt
I am oldboy
I am oldboy teacher!
[root@zhangxiangyu-58 ~]# egrep -w "oldboy" oldboy.txt
I am oldboy
I am oldboy teacher!
[root@zhangxiangyu-58 ~]# egrep "\boldboy" oldboy.txt
I am oldboy
I am oldboy teacher!
our site is http://www.oldboyedu.com
\d 匹配单个数字字符
[root@zhangxiangyu-58 ~]# grep -P "\d" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@zhangxiangyu-58 ~]# grep -Po "\d" oldboy.txt
4
9
0
0
0
4
4
8
4
9
0
0
0
0
0
4
4
8
Sed:stream editor 流编辑器(三剑客老二)
对文件的 增加 删除 修改 查看
常用的两大功能是过滤(过滤指定字符)和取行(取出指定行)
选项:
-n 取消命令的默认输出
-i 直接修改文件内容,而不是输出到终端
Sed的内置命令字符说明:
s 替换
d 删除delete
g 全局global
p 打印print
打印行:
-n 默认不输出,把你需要的输出
[root@zhangxiangyu-58 ~]# cat -n oldgirl.txt
1 I am oldboy teacher!
2 I like badminton ball ,billiard ball and chinese chess!
3 our site is http://www.oldboyedu.com
4 my qq num is 49000448.
1,输出oldboy.txt中的第2-3行内容
[root@zhangxiangyu-58 ~]# sed -n '2,3p' oldgirl.txt
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
2,过滤出含oldboy字符串的行
[root@zhangxiangyu-58 ~]# sed -n '/oldboy/p' oldgirl.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com
3,删除含有oldboy字符串的行
[root@zhangxiangyu-58 ~]# sed '/oldboy/d' oldgirl.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 49000448.
4,将文件中的oldboy字符串全部替换为oldgirl
用vim :%s/oldboy/lodgirl/g
Sed ‘s#把什么替换#替换成什么#g’ oldgirl.txt
[root@zhangxiangyu-58 ~]# sed 's#oldboy#oldgirl#g' oldgirl.txt
I am oldgirl teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldgirledu.com
my qq num is 49000448.
5,将文件中的oldboy字符串全部替换为oldgirl,同时将QQ号码49000448改为31333741
-e
[root@zhangxiangyu-58 ~]# sed -e 's#oldboy#oldgirl#g' -e 's#49000448#31333741#g' oldgirl.txt
I am oldgirl teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldgirledu.com
my qq num is 31333741.