Grep and What If 75

1

I'm trying to run the command from What if 75:

 cat wordlist.txt | perl -pe 's/^(.*)$/\L$& \U$&/g' | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' '2223334445556667777888999' | grep -P "(.)\1\1\1\1\1"

I replaced wordlist.txt with /usr/share/dict/words because I'm on OS X.

When I run the command, all I get is grep's usage message. If I run the command with the grep portion removed, it seems to be doing what should.

How can I edit the command to make it work correctly?

EDIT: I replaced grep -P with egrep and it worked fine.

dejay

Posted 2014-03-23T00:49:52.130

Reputation: 125

Answers

1

The code from xkcd uses GNU grep (many Linux distros), not BSD grep (Mac OS X).

GNU grep has some features BSD grep doesn't, e.g. Perl-compatible Regular Expressions (PCRE).

The pattern in your example, (.)\1\1\1\1\1, actually works for both PCRE and Extended Regular Expressions (ERE); if you modify the syntax slightly, you can even use Basic Regular Expressions (BRE):

grep -P "(.)\1\1\1\1\1"
grep -E "(.)\1\1\1\1\1"
grep "\(.\)\1\1\1\1\1"

all work as expected, as long as the feature is available. The latter two will work on OS X or any other implementation without GNU extensions.

For a formal definition of BRE and ERE, click here.

For the differences between BRE, ERE and PCRE, consult Why does my regular expression work in X but not in Y?.

Dennis

Posted 2014-03-23T00:49:52.130

Reputation: 42 934