Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

A cool trick that saves you from having to use grep -v when grepping ps output (yes I know pgrep exists) is to put the first letter of the process into a character class like ps -ef |grep [s]vn. This will exclude the grep svn from being included in the output. Why does this work? GNU grep.

share|improve this question
    
Please look at this thread ;-) serverfault.com/questions/367921/… –  jamzed May 22 '13 at 20:08
    
Sounds totally plausible, and kind of what I figured. If you know this is correct, put it in an answer, not a comment so I can choose it. –  user160910 May 22 '13 at 20:08

2 Answers 2

up vote 3 down vote accepted

Grep is searching for a regular expression, so for example [Pp]rocess will find Process or process, but an interesting side effect is that the command line of the grep operation has a ] stuck in there, which means that the grep line doesn't contain the word process, it contains the word p]rocess, which doesn't match.

So effectively you're removing the word process from your grep line by putting the closing bracket in there, but still allowing it to match the word on other lines.

share|improve this answer

Square brackets are part of grep's pattern matching. Basically grep gets basic regex:

[12p]rocess will expand to: 1rocess 2rocess process

Now, grep will actually search for [1]rocess [2]rocess [p]rocess which - as you can see - it's not process.

Square brackets are character class matching in regular expressions. You can read more about regualar expressions and character matching here:

http://www.regular-expressions.info/charclass.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.