Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Character classes not working in grep command

Seems grep / egrep does not support character classes, e.g.

# find -type f | grep -e '[\d]'

It finds all the file contains the character d, instead of decimal number(s).

Answer:

By default, egrep only understand the POSIX Basic Regular Expressions (BRE) standard, so what would like to use is:

# find -type f | grep -e '[[:digit:]]'

or you can force to interpret as a Perl regular expression

# find -type f | grep -P -e '[\d]'

  1. How to grep for tab character(s) in Linux
  2. Only show filenames in grep command
  3. Capturing group difference in Basic Regular Expressions (BRE) and Extended Regular Expressions (ERE)
  4. Filter a list using grep in Perl
  5. xargs when the filename contains a newline

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>