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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
gnerd
  • 3
  • 1
  • On the topic, `[:space:]` matches tabs, newlines, vertical tabs, form feeds, carriage returns, and spaces, and `[:blank:]` matches spaces and tabs. – Christopher Aug 06 '19 at 18:57
  • @Christopher With GNU `grep`, `\s` is a synonym for `[[:space:]]`, so the conversion is correct. – Kusalananda Aug 06 '19 at 19:00

2 Answers2

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:

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
1

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

Zach Sanchez
  • 304
  • 1
  • 5
  • 1
    Do note that POSIX is/has been moving towards a single `grep`, away from grep/fgrep/egrep. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html – Jeff Schaller Aug 06 '19 at 18:59
  • Oh really? I wasn't aware of that, thanks for the heads up. – Zach Sanchez Aug 06 '19 at 19:03
  • The [GNU grep man page](https://manpages.ubuntu.com/manpages/bionic/en/man1/grep.1.html) is explicit, saying `egrep` etc are deprecated. – glenn jackman Aug 07 '19 at 23:18