Ignore grep command self-match

Aug 05, '05 08:21:00AM

Contributed by: al.cameron

Here's a simple Unix tip, inspired by a comment Rob made on a recent hint:

...When I ran ps ax | grep Dash ... (OK, technically, it found the grep, but that's because I'm too lazy to add it as an ignored match)...
You can use a simple regular expression, instead of adding another grep -v pipe element, to ignore the self-match when grep-ing for a literal string. That is, rather than this:
ps ax | grep Dash | grep -v grep
Use this:
ps ax | grep [D]ash
The regular expression (the D inside the square brackets) matches a character set which contains a single character (upper case D in this case) and the literal string "ash." So this grep can't self-match the original regular expression string, as it also includes the square brackets.

[robg adds: Yep, this is a simple tip, but since regular expressions are not something I know anything about, I appreciate the timesaver ... so I figured it was worth sharing with other relative Unix newcomers...]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20050729121017774