1、什么是正则表达式?
作用和特殊字符一样。
正则表达式是为处理大量的字符串及文本而定义的一套规则和方法。
假设"@"代表“I am”,"!"代表“oldboy”,
则执行echo "@!"的结果就是输出“I am oldboy”。
3、适用于三剑客命令 grep(egrep),sed,awk
以行为单位处理。
易混淆事项
1、和通配符区别。
2、开发人员正则,一般是Perl兼容正则表达式。
3、Linux系统三剑客正则表达式******。
正则表达式分类:
1、BRE grep
2、ERE egrep
[root@oldboyedu ~/test]# grep "^I" oldboy.txt #以i开头的
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
[root@oldboyedu ~/test]# ls /data
a.txt aa.txt b.txt c.txt f.txt test.txt
[root@oldboyedu ~/test]# mkdir /data/oldboy
[root@oldboyedu ~/test]# ls -l /data
total 0
-rw-r--r--. 1 root root 0 Oct 4 23:28 a.txt
-rw-r--r--. 1 root root 0 Oct 4 23:28 aa.txt
-rw-r--r--. 1 root root 0 Oct 4 23:28 b.txt
-rw-r--r--. 1 root root 0 Oct 4 23:38 c.txt
-rw-r--r--. 1 root root 0 Oct 4 23:38 f.txt
drwxr-xr-x. 2 root root 6 Oct 5 01:08 oldboy
-rw-r--r--. 1 root root 0 Oct 4 23:28 test.txt
[root@oldboyedu ~/test]# ls -l /data|grep "^d" # 查找data目录里的文件
drwxr-xr-x. 2 root root 6 Oct 5 01:08 oldboy
代表 自身是 .代表任意一个字符。
匹配 ? 匹配前一个字符。
es? 匹配e es #?查一个
es* 匹配e es ess essssss essssssssss 更多s。 # *多个
e*s* 和e?s?
e* 空 e ee eeee eeeeee
s* 空 s ss sss ssssssssssssss
e*s* 空 es e s ees essssss
e?s? 空 e s es
e? 空 e
s? 空 s
e+ e ee eee eeeee .........
e* 空 e ee eeee eeeeee .........
e? 空 e
a{n,m} 匹配前一个字符最少n次,最多m次
a{n,} 匹配前一个字符最少n次
a{n} 匹配前一个字符正好n次
a{,m} 匹配前一个字符最多m次
(0)===\1
(0)(0) \1 \2
第一个括号 第二个括号
egrep -o "(e)(s)\1\2" oldboy.txt ===== egrep -o "eses" oldboy.txt
特殊预定义中括号表达式
[root@oldboyedu ~/test]# egrep "[0-9]" oldboy.txt #有数字的行
my qq num is 49000448.
not 4900000448.
[root@oldboyedu ~/test]#
[root@oldboyedu ~/test]# egrep "[[:digit:]]" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu ~/test]# egrep "[[:lower:]]" oldboy.txt
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@oldboyedu ~/test]# egrep "[[:upper:]]" oldboy.txt
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu ~/test]# egrep "\boldboy\b" oldboy.txt
I am oldboy teacher!
[root@oldboyedu ~/test]# egrep "oldboy" oldboy.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com
[root@oldboyedu ~/test]# egrep -w "oldboy" oldboy.txt
I am oldboy teacher!
评书:三侠剑 老好了。
侠客、剑客
Linux三剑客
awk sed grep
sed
Sed是操作、过滤和转换文本内容的强大工具。
常用功能有对文件实现快速增删改查(增加、删除、修改、查询),
其中查询的功能中最常用的2大功能是过滤(过滤指定字符串)和取行(取出指定行)。
sed [选项] [sed内置命令字符] [文件]
选项:
-n 取消默认sed的输出,常与sed内置命令的p连用※
-i 直接修改文件内容,而不是输出到终端。
如果不使用-i选项sed只是修改在内存中的数据,并不会影响磁盘上的文件※
sed的内置命令字符说明
s 替换
g 全局global
p 打印print
d 删除delete
考题 sed的
环境:
[root@oldboyedu ~/test]# cat oldgirl.txt
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.
问题1:输出oldboy.txt的第2-3行内容※。
sed -n "2,3p" oldgirl.txt
问题2:过滤出含有oldboy字符串的行※。
sed -n "/oldboy/p" oldgirl.txt问题
问题3:删除含有oldboy字符串的行※。
sed "/oldboy/d" oldgirl.txt
问题4:将文件中的oldboy字符串全部替换为oldgirl※。
sed "s#oldboy#oldgirl#g" oldgirl.txt
问题5:将文件中的oldboy字符串全部替换为oldgirl,同时将QQ号码49000448改为31333741。
sed -e "s#oldboy#oldgirl#g" -e "s#49000448#31333741#g" oldgirl.txt
问题6:在oldboy.txt文件的第2行后追加文本。
sed -i '2a sssss' oldgirl.txt #在第二行的下一行加东西
问题7:在oldboy.txt文件的第2行插入文本。
sed -i '2i sssss' oldgirl.txt#在第二行的下上行加东西
删除指定行
sed -i '3d' oldgirl.txt
sed -i '5,8d' oldgirl.txt
详解;环境:
[root@oldboyedu ~/test]# cat oldgirl.txt
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.
问题1:输出oldboy.txt的第2-3行内容※。
问题2:过滤出含有oldboy字符串的行※。
问题3:删除含有oldboy字符串的行※。
问题4:将文件中的oldboy字符串全部替换为oldgirl※。
问题5:将文件中的oldboy字符串全部替换为oldgirl,同时将QQ号码49000448改为31333741。
问题6:在oldboy.txt文件的第2行后追加文本。
问题7:在oldboy.txt文件的第2行插入文本。
删除指定行
sed -i '3d' oldgirl.txt
sed -i '5,8d' oldgirl.txt
环境:
[root@oldboyedu ~/test]# cat oldgirl.txt
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.
问题1:输出oldboy.txt的第2-3行内容※。
[root@oldboyedu ~/test]# sed -n '2,3p' oldgirl.txt
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
[root@oldboyedu ~/test]# head -3 oldgirl.txt |tail -2
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
问题2:过滤出含有oldboy字符串的行※。
[root@oldboyedu ~/test]# sed -n '/oldboy/p' oldgirl.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com
[root@oldboyedu ~/test]# grep oldboy oldgirl.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com
问题3:删除含有oldboy字符串的行※。
[root@oldboyedu ~/test]# sed '/oldboy/d' oldgirl.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 49000448.
[root@oldboyedu ~/test]# grep -v "oldboy" oldgirl.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 49000448.
问题4:将文件中的oldboy字符串全部替换为oldgirl※。
[root@oldboyedu ~/test]# 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.
vim替换:
问题5:将文件中的oldboy字符串全部替换为oldgirl,同时将QQ号码49000448改为31333741。
[root@oldboyedu ~/test]# sed -e 's#oldboy#oldgirl#2' -e 's#49000448#31333741#g' oldgirl.txt
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 31333741.
问题6:在oldboy.txt文件的第2行后追加文本。
[root@oldboyedu ~/test]# sed '2a I teacher linux.' oldgirl.txt
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
I teacher linux.
our site is http://www.oldboyedu.com
my qq num is 49000448.
[root@oldboyedu ~/test]# cat oldgirl.txt
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.
[root@oldboyedu ~/test]# sed -i '2a I teacher linux.' oldgirl.txt
[root@oldboyedu ~/test]# cat oldgirl.txt
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
I teacher linux.
our site is http://www.oldboyedu.com
my qq num is 49000448.
[root@oldboyedu ~/test]# sed '2i I teacher linux.i' oldgirl.txt
I am oldboy teacher!
I teacher linux.i
I like badminton ball ,billiard ball and chinese chess!
I teacher linux.
our site is http://www.oldboyedu.com
my qq num is 49000448