0

I have this egrep command which's expression searchs for filenames with length 3 (excluding .extension):

ls | grep -E '^.{3}\.+'

However, I'm unable to write this with grep only, is it possible?

4
  • 3
    Why grep, why not ls ???.*?
    – iruvar
    Commented Oct 10, 2015 at 22:04
  • 1
    That should work. Note though, that the "+" is not needed (asks for 1 or more periods at the end, but can be followed by anything). Also - @1_CR's answer makes a lot more sense...
    – Brian
    Commented Oct 10, 2015 at 22:10
  • I know I know @1_CR , I just really want to know how to do it with grep, curiosity..
    – yat0
    Commented Oct 10, 2015 at 22:13
  • 1
    The BRE equivalent would be grep '^.\{3\}\.\+' I think (i.e. escape the braces and the +) Commented Oct 10, 2015 at 22:20

1 Answer 1

3

Try this:

ls | grep '^.\{3\}\.\+'

Basic vs Extended Regular Expressions

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, +, {, \|, (, and ).

(from man grep)

(I can see that steeldriver beat me to it by some 30s while I was formulating the answer.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .