元字符
查找命令egrep(grep的进阶版)
egrep “a.b” filename.txt(包含字符a、b的行,有顺序:a在b前面)
egrep “a*b” filename.txt(同上,只是没顺序要求)
egrep “a+b” filename.txt(包含a和b的行)
egrep “[aeiou]” filename.txt(包含中括号内任意字符的行)
egrep “[^aeiou]” filename.txt(不包含中括号内任意字符的行)
egrep “[e-q]” filename.txt(包含从e到q的任意字符的行)
egrep “/w” filename.txt(/w是包含word的行,/d是数字,/s是空格,大写字母是反义,同加上-v,eg:egrep -v “\w” filename.txt = egrep “/W” filename.txt)
egrep “.” filename.txt(包含”.”的行,即反斜杠起了转义的作用)
egrep “^M” filename.txt(以M开头的行)
egrep “M$” filename.txt(以M结尾的行)
egrep “time|aa” small.txt(包含time或者aa的行)
egrep -n “time” small.txt(-n展示包含time的行的行号)
egrep “New” states.txt canada.txt(一次搜索多个文件)
egrep “s{2}” states.txt(s出现2次)
egrep “s{2,3}” states.txt(s出现2到3次)
egrep “(iss){2}” states.txt(iss出现2次)
| Metacharacter | Meaning |
|---|---|
| . | Any Character |
| \w | A Word |
| \W | Not a Word |
| \d | A Digit |
| \D | Not a Digit |
| \s | Whitespace |
| \S | Not Whitespace |
| [def] | A Set of Characters |
| [^def] | Negation of Set |
| [e-q] | A Range of Characters |
| ^ | Beginning of String |
| $ | End of String |
| \n | Newline |
| + | One or More of Previous |
| * | Zero or More of Previous |
| ? | Zero or One of Previous |
| | | Either the Previous or the Following |
| {6} | Exactly 6 of Previous |
| {4, 6} | Between 4 and 6 or Previous |
| {4, } | More than 4 of Previous |
本文介绍了使用egrep命令进行文本搜索的方法,包括各种元字符的意义及用法,如.、*、+等,并通过实例展示了如何利用这些元字符进行精确匹配。

被折叠的 条评论
为什么被折叠?



