正则表达式在grep中的使用
GREP用法
grep “after” profile #查找文件内的包含after单词的行
grep -n "after" profile #添加查找的行在文档的行号
grep -v "after" profile #查找文件内的不包含after单词的行
匹配符:
-
\ 转义字符
-
. 匹配任意单个字符
-
[1234abc],[^],],[1-5],[a-d] 字符序列单字符占位
grep '[b-d]' hello.txt # 查询包含bcd字符的字符串 -
^ 行首
-
$ 行尾
-
\<,\>,\<abc,abc\>,\<are\> 单词首位边界 okhelloworld ok hello world
-
| 连接操作符
-
(,) 选择操作符
-
\n 反向引用
正则表达式在grep中的使用
#基本匹配 *它前面的字符出现0到多次
grep "a*re" hello.txt # 从hello.txt中查找以a开头,以re结尾,a出现0到多次的字符串
#扩展匹配 注意:扩展匹配使用前要加-E选项;或者用转义字符
grep "a+re" hello.txt
grep "a\+re" hello.txt #效果同上
#1. ?它前面的字符出现0到1次
#2. +它前面的字符出现1到多次
#3. {n}它前面的字符出现n到多次
#4. {n,}它前面的字符出现n到多次
#5. {m,n}它前面的字符出现m到n次

浙公网安备 33010602011771号