0

我们可以在 grep 中搜索像 "\w\w\w\d" 这样表示三个字母后跟一个数字的模式吗?它不工作。linux终端有没有办法做同样的事情?

例如我想匹配'ABC9'或'NMJ6'等

4

3 回答 3

3

例如:

回声ABC9 | grep -E '[[:alpha:]]{3}[[:digit:]]' -

这利用了由以下定义的字符类man grep

   Finally,  certain  named  classes  of  characters  are predefined
   within bracket expressions, as follows.   Their  names  are  self
   explanatory,   and  they  are  [:alnum:],  [:alpha:],  [:cntrl:],
   [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:],
   [:upper:],   and  [:xdigit:].   For  example,  [[:alnum:]]  means
   [0-9A-Za-z], except the latter form depends upon the C locale and
   the  ASCII  character encoding, whereas the former is independent
   of locale and character set.  (Note that the  brackets  in  these
   class  names are part of the symbolic names, and must be included
   in addition to the brackets delimiting the  bracket  expression.)
   Most  meta-characters  lose  their special meaning inside bracket
   expressions.  To include a literal ] place it first in the  list.
   Similarly,  to  include  a literal ^ place it anywhere but first.
   Finally, to include a literal - place it last.
于 2012-10-25T18:39:42.940 回答
2

grep -P "\w\w\w\d"

-P, --perl-regexp
Interpret PATTERN as a Perl regular expression.
于 2012-10-25T18:35:54.007 回答
2
grep -P '\w\w\w\d'

添加 --color 使其真正脱颖而出。例如:

echo 'blahblahABC9blahblah' | grep --color -P '\w\w\w\d'
于 2012-10-25T18:36:52.853 回答