以Grep学正则表达式 学习笔记

基本格式

grep -n -A2 -B3 --color=auto 'the' ./

搜寻特定字符串

grep -n 'the' //含
grep -vn 'the' //不含
grep -in 'the' //含大小写

利用中括号 [] 来搜寻集合字符

grep -n 't[ae]st' //含tast test
grep -n '[^g]oo' //含oo但oo前不是g
grep -n '[^a-z]oo' //含oo但oo前不是ab..z 等同于 grep -n '[^[:lower:]]oo' 
grep -n '[^a-zA-Z0-9]oo' //含oo但oo前不是ab..z也不是AB..Z也不是01..9

行首与行尾字符 ^ $

grep -n '^the' //以the作为行首
grep -n '\.$' //以.结尾
grep -n '^$' //空行

任意一个字符 . 不重复字符 *

grep -n 'g..d' //good,glad..中间必须两个字符
grep -n 'g*d' //g有0个或多个并以d结尾
grep -n 'g.*d' //g开头中间任意个字符并以d结尾

限定连续 RE 字符范围 {}

grep -n 'g\{2\}d' //good,glad..中间必须两个字符
grep -n 'g\{2,5\}d' //good,glad..中间2-5个字符
grep -n 'g\{2,\}d' //good,glad..中间2个以上字符

猜你喜欢

转载自blog.csdn.net/panbinxian/article/details/82908485