0

grep で 3 文字の後に数字が続く「\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 に答える