正则表达式在grep中的使用

GREP用法

grep “after” profile #查找文件内的包含after单词的行
grep -n "after" profile #添加查找的行在文档的行号
grep -v "after" profile #查找文件内的不包含after单词的行

匹配符:

  1. \ 转义字符

  2. . 匹配任意单个字符

  3. [1234abc],[^],],[1-5],[a-d] 字符序列单字符占位

    grep '[b-d]' hello.txt  # 查询包含bcd字符的字符串
    
  4. ^ 行首

  5. $ 行尾

  6. \<,\>,\<abc,abc\>,\<are\> 单词首位边界 okhelloworld ok hello world

  7. | 连接操作符

  8. (,) 选择操作符

  9. \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次
posted @ 2021-10-04 13:00  程胥员  阅读(80)  评论(0)    收藏  举报