[小知识] grep中如何使用正则表达式匹配数字

如何使用grep搜索包含数字的行?

记得“\d”可以表示任意数字,遂写成grep "\d" test.txt 


没有返回任何数据, 难道不支持“\d”


你在命令行下执行man grep 查看帮助,可以看到grep是支持多种正则表达式的,需要用参数去切换。

Matcher Selection
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)

       -F, --fixed-strings
              Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.  (-F is specified by POSIX.)

       -G, --basic-regexp
              Interpret PATTERN as a basic regular expression (BRE, see below).  This is the default.

       -P, --perl-regexp
              Interpret  PATTERN as a Perl regular expression (PCRE, see below).  This is highly experimental and grep -P may warn of unimplemented
              features.

而默认情况下,grep只支持basic regex

“\d” 不被支持是因为它不属于basic regex。


那么basic regex有哪些?“\d” 又属于哪类正则表达式呢?

请参考这里:http://www.zytrax.com/tech/web/regex.htm

你可能感兴趣的:(入门技术,linux,shell)