Grepping for parentheses

Just because I had to do this today and thought I’d share. Either use the -E option, for example,

grep -E "let \(" */*.swift

or use egrep directly:

egrep "let \(" */*.swift

Both egrep and grep -E use extended regular expressions, allowing you to use a simple backslash rather than trying to get your shell to cooperate with hyper-escaping. You can use the same approach with sed as well:

echo "(hi)" | sed -E "s/\(/[/"

I hope this helps someone.

3 Comments

  • You can also grep for the set of characters that consists solely of the left parenthesis.

    % grep ‘let [(]’ */*.swift

  • Try `fgrep` or `grep -f` 🙂

  • Also, if you ever write a shell script that needs to run on multiple platforms, keep in mind that while OS X (BSD) uses `sed -E` for extended regexes, Linux (GNU) uses `sed -r`.