最常应用正则表达式命令是 awk sed grep
[[email protected] ~]# cat mike.log I am mike! I like linux. I like play football, tennis and reading. my qq is 123456789! my blog is https://i.cnblogs.com my blog is i.miek.com I am miek. not MIEK NOT MIKE
基础正则字符说明
1.^word 匹配以word开头的内容 vi/vim编辑器里^代表一行的开头
2.word$ 匹配以word结尾的内容 vi/vim编辑器里$代表一行的结尾
3. ^$ 表示空行
a.过滤出来以m开头的行
[[email protected] ~]# grep "^m" mike.log my qq is 123456789! my blog is https://i.cnblogs.com my blog is i.miek.com
b.过滤出来以m结尾的行
[[email protected] ~]# grep "m$" mike.log my blog is https://i.cnblogs.com my blog is i.miek.com
c.过滤匹配空行的行
grep -n 显示匹配的行号
[[email protected] ~]# grep -n ‘^$‘ mike.log 3: 11:
grep -v 排除内容 指定字段不显示
过滤出 不带 空行的行,其他行都打印
[[email protected] ~]# grep -vn "^$" mike.log 1:I am mike! 2:I like linux. 4:I like play football, tennis and reading. 5:my qq is 123456789! 6:my blog is https://i.cnblogs.com 7:my blog is i.miek.com 8:I am miek. 9:not MIEK 10:NOT MIKE
4. . 代表且只能代表任意一个字符。
5. \ 转义
6. * 重复0个或多个前面的一个字符 例如o* 匹配没有o,有一个o或多个o
7 .* 匹配所有字符 ^.*以任意多个字符开头, .*$ 以任意多个字符结尾
a. 匹配任意一个字符 .
因为第三行没有字符 没有匹配到
[[email protected] ~]# grep -n "." mike.log 1:I am mike! 2:I like linux. 4:I like play football, tennis and reading. 5:my qq is 123456789! 6:my blog is https://i.cnblogs.com 7:my blog is i.miek.com 8:I am miek. 9:not MIEK 10:NOT MIKE
.*
[[email protected] ~]# grep -n ".*" mike.log 1:I am mike! 2:I like linux. 3: 4:I like play football, tennis and reading. 5:my qq is 123456789! 6:my blog is https://i.cnblogs.com 7:my blog is i.miek.com 8:I am miek. 9:not MIEK 10:NOT MIKE 11:
匹配 mi.. .. 任意字符
[email protected] ~]# grep -n "mi.." mike.log 1:I am mike! 7:my blog is i.miek.com 8:I am miek.
grep -i 不区分大小写
[[email protected] ~]# grep -ni "mi.." mike.log 1:I am mike! 7:my blog is i.miek.com 8:I am miek. 9:not MIEK 10:NOT MIKE
匹配以.结尾 需要转义
[[email protected] ~]# grep -n "\.$" mike.log 2:I like linux. 4:I like play football, tennis and reading. 8:I am miek.
-o 只显示匹配的内容
[[email protected] ~]# grep -no "mi.." mike.log 1:mike 7:miek 8:miek
8. [abc] 匹配字符串集合内任意一个字符 [a-zA-Z],[0-9]
9. [^abc] 匹配不包含^后的任意一个字符的内容
中括号里的^为取反
匹配0-9数字的行 匹配数字的行
[[email protected] ~]# grep "[0-9]" mike.log my qq is 123456789!
原文地址:https://www.cnblogs.com/mingerlcm/p/10930900.html
时间: 2024-10-30 08:36:41