Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Ignore grep command self-match' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Ignore grep command self-match
Authored by: gshenaut on Aug 06, '05 12:40:43PM
One of the advantages of the original hint is that it avoids the second grep process in the pipeline. However, its form, which requires separating the first letter from the others, makes difficult to embed in an alias. This is why the other suggestions go back to the two-process pipeline.

However, the general principle is that the regular expression not match its invocation. Since the names of programs are always "words" in the regular expression sense, there is another way to use the basic principle of the original hint, but which allows easy use in an alias: explicitly match the word boundary using the \b regular expression substring. This results in a command

grep \\bDash
for the original example, and since it is trivial to precede any string with \b or \b depending on the quote level, this can be used easily in a shell function or script:
function psg () { ps ax | grep \\b$1 ; }
Note that this can't be an alias because "$1" expands to " $1" when aliases are expanded, with an added space that messes up the regular expression. But functions work fine.

Greg Shenaut

[ Reply to This | # ]