0

I'm trying to abbreviate a regex in a grep. I need to match exactly six spaces followed by an alpha character. This works:

grep "^\s\s\s\s\s\s[[:alpha:]]" <filename>

This does not:

grep "^[[:space:]]{6}[[:alpha:]]" <filename>

What am I doing wrong?

2
  • On the topic, [:space:] matches tabs, newlines, vertical tabs, form feeds, carriage returns, and spaces, and [:blank:] matches spaces and tabs. Aug 6, 2019 at 18:57
  • @Christopher With GNU grep, \s is a synonym for [[:space:]], so the conversion is correct.
    – Kusalananda
    Aug 6, 2019 at 19:00

2 Answers 2

6

{6} is an extended regular expression "bound" that won't work in basic regular expressions (it would match {6} literally). The grep utility is using basic regular expressions by default.

Two solutions:

  1. Use \{6\} instead, which is how you'd write it in a basic regular expression.
  2. Use grep -E, which enables the use of extended regular expressions in grep.

Also, if you want to match spaces (and no other characters; [[:space:]], as well as \s in GNU grep, matches space, vertical/horizontal tab, form feed, newline, and carriage return), use a literal space. For example,

grep -E '^ {6}[[:alpha:]]'

Related:

1

As an addendum to what Kusalananda said, you could also use egrep if you don't feel like dealing with the -E flag.

3

You must log in to answer this question.

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