基本正则表达式
扩展正则表达式
结合grep的用法
sed命令
写笔记参考博客
http://apecome.com:9494/03%E7%B3%BB%E7%BB%9F%E6%9C%8D%E5%8A%A1%E7%AF%87/3-16-%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E5%92%8C%E4%B8%89%E5%89%91%E5%AE%A2.html
正则练习题参考博客
url
1.看懂老师写的正则,参考,思路的提供
2.自己再去写出另一种正则,即使你就是多了一个字符
测试数据:
I am teacher yuchao.
I teach linux,python.
testtttsss000123566666
I like video,games,girls
#I like girls.
$my blog is http://yuchaoit.cn/
#my site is https://www.cnblogs.com/pyyu
My qq num is 877348180.
my phone num is 15210888008.
^
查找以什么开头的行I
开头的行grep -n '^I' test.txt
'^字符'
$
查找以什么结尾的行u
结尾的行结尾字符$
grep -n 'u$' test.txt
.
结尾的行为了最大程度的降低,字符的特殊含义,别用双引号,否则你要考虑加很多的转义符
grep '\.$' -n t1.log
grep -n '\.$' test.txt
^
$
^$ 提取出空白行,没有内容的行,连空格都没有的
I
开头,s
结尾的行grep -n '^I.*s$' test.txt
Is
Ias
Iaaaaaaaaaaaas
IbS
I qs
# 匹配空行的正则
grep -n '^$' test.txt
# grep -v结果取反
grep -nv '.' test.txt
grep -n '.' test.txt
grep -n -v '^$' test.txt
.
任意一个字符 不会匹配空行 、可以匹配空格,注意这里的点是正则符、而不是普通的点grep -n '.' test.txt
\
转义特殊字符找出以$开头的行
grep -n '^\$' test.txt
#暂时武略这种写法
grep -n "^\\s" test.txt
grep -n '[a-z]' test.txt
grep -n '[[:lower:]]' test.txt
.
或s
结尾的行grep -E '\.$|s$' t1.log
egrep '\.$|s$'
grep -E '(\.|s)$' t1.log
grep -E '[.s]$' t1.log
grep -E '[\.s]$' t1.log
grep -n '[.s]$' test.txt
I
开头,且结尾不是.
的行grep -E '^I.*[^\.]$' t1.log
grep -n '^I.*[^.]$' test.txt
grep -i '^i' test.txt
准备测试数据
I am teacher yuchao.
I teach linux,python.
testtttsss000123566666
I like video,games,girls
#I like girls.
$my blog is http://www.yuchaoit.cn
#my site is https://www.cnblogs.com/pyyu
My qq num is 877348180.
my phone num is 15210888008.
往复杂了思考
中国电信号段133、153、173、177、180、181、189、191、193、199
中国联通号段130、131、132、155、156、166、175、176、185、186、166
中国移动号段134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、172、178、182、183、184、187、188、198
手机号码的规则如下:
第一位: 1
第二位: 3~9
第三到第十一位只要是数字就行
[242-yuchao-class01 root ~]#grep -E '1[3-9][0-9]{9}' t2.log -o
15210888008
grep 参数
-E 扩展正则
-o 只显示结果
-w 匹配单词(单词表示出现了分割符如,hello,i am super mam.)
grep -Enow '[0-9]{11}' test.txt
grep -Enow '[0-9]{9}' test.txt
找出至少有2个连续的相同字母,且只能找出至少2次连续的字母
向后引用,提取
这个写法是,括号内的数据,必然要再出现一次
这个写法是,只找出2个字母
grep -E '([a-zA-Z])\1' t2.log
ww
wwwwww
分组、向后引用、+号 组合用法
grep -E '([a-zA-Z])\1+' t2.log
只想提取三个连续一样的字母
两种写法
grep -E '([a-zA-Z])\1{2}' t2.log
grep -E '([a-zA-Z])\1\1' t2.log
括号分组,向后引用,语法还未理解
grep -En '([a-z])\1{2}' test.txt
grep -Eno '([a-z])\1{2,}\.[a-z0-9]{1,}\..*' test.txt
grep -Eno '([a-z])\1{2,}\.[a-z0-9]{1,}\.[a-z0-9]{1,}' test.txt
# 参考写法
[242-yuchao-class01 root ~]#grep -E 'www\..*\.(cn|com)' -o t2.log
www.yuchaoit.cn
www.cnblogs.com
[242-yuchao-class01 root ~]#grep -E 'www\..*\.c(n|om)' -o t2.log
www.yuchaoit.cn
www.cnblogs.com
grep -E 'http[s]?://([a-zA-Z0-9])\1{2}\.[a-zA-Z0-9]{1,}\.[a-zA-Z0-9]{1,}[/]?[a-zA-Z0-9]{0,}' test.txt
grep -E 'h.*://(.*).(.*).(.*)[/]?.*' test.txt
grep -E 'h.*://.*[/]?.*' test.txt
# 提取https://www.xxx.com/xxxxxxxxxxxxxx
[242-yuchao-class01 root ~]#grep -E 'http[s]?://www\..*\..*' -o t2.log
http://www.yuchaoit.cn
https://www.cnblogs.com/pyyu
[242-yuchao-class01 root ~]#grep -E 'http[s]?://www\..*\.(cn|com).*' -o t2.log
http://www.yuchaoit.cn
https://www.cnblogs.com/pyyu
grep -Ev '^#|^$' test.txt
grep -v '^#' test.txt | grep '.'
grep -v '^#' chaoge.txt | grep -v '^$'
ps -ef |grep ssh |grep -v 'grep'
df -h |grep sda |grep -oE '[0-9]+%'
df -h|grep '/$' |grep -oE '[0-9]+%'
思路:找到命令中的ip相关,过滤出ip,显示指定ip
ifconfig命令
ifconfig ens33 |grep 'netmask' | grep -Eo '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' | head -1
ip addr show命令
ip addr show ens33 | grep 'ens33$' | grep -Eo '[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' | head -1
思路:过滤出/etc/passwd 中的nologin项 ,使用grep的-o参数只显示过滤出的用户名
grep 'nologin' /etc/passwd |grep -Eo '^[a-z0-9A-Z]+'
root
useradd创建普通用户
uid 从1000开始
思路:root创建的用户从UID从1000开始,过滤出UID超过1000的用户
grep -E '\b[0-9]{4,}\b' /etc/passwd