1.grep中正则的用法
^ 匹配行首
$ 匹配行尾
[] 集合,匹配集合中的任意单个字符
[^] 对集合取反
. 匹配任意单个字符
* 匹配前一个字符任意次数(包括0次) //grep “ro*ot” txt
txt中的内容:
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
grep "ro\{2,3\}t" txt //找root或者rooot
grep "ro\{2\}t" txt
grep "ro\{3\}t" txt //找rooot
grep "ro\{1\}t" txt //找rot
grep "ro\{1,\}t" txt //找rot,o可能出现2次或者两次以上
grep "[a-z]" txt //找小写字母的行
grep "[^a-z]" txt //找大写字母的行
grep "[A-Z]" txt //找大写字母的行
grep "[^A-Z]" /etc/shadow //找拥有除了大写字母的行
grep "[a-Z]" /etc/shadow //找所有字母
grep "[^a-Z]" /etc/shadow //不找字母
grep "[0-9]" /etc/shadow //找数字
grep roo. txt //找roo开头,后面追加1个任意字符的行
grep ro.. txt //找ro开头,后面追加2个任意字符的行
grep "*" txt //不能单独使用什么也找不到
grep .oot txt //找某字符开头后面是oot的行
grep ro*ot txt //找rot,第一个o可以出现任意次
grep ro*t txt //找rot,o可以出现任意次
grep bo*i txt //找boi,o可以出现任意次
grep ".*" txt //找任意,比下面的多保函真正的空白行
grep "." txt //找任意单个字符
2.扩展正则
+ 匹配前一个字符至少一次 grep -E "ro+t" txt等同于 egrep "ro+t" txt grep天生不支持正则
? 匹配前面的字符最多一次 egrep "ro?ot" txt 等同于 grep "ro\{0,1\}" txt
{n,m} 匹配前面的字符n到m次 egrep "ro{2,3}ot" txt
| 或者 egrep "r|o|t"
\b 单词边界 egrep "\bbin\b" 空白、百分号、空格、⭐号、tab、特殊符号都不是边界