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

Ignore grep command self-match UNIX
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...]
    •    
  • Currently 3.83 / 5
  You rated: 3 / 5 (6 votes cast)
 
[21,233 views]  

Ignore grep command self-match | 9 comments | Create New Account
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: lar3ry on Aug 05, '05 10:24:16AM
Here's an easier way:

$ ps -acx | grep Dash

The "c" option tells ps to only show the command, not the command line arguments. Thus, the "grep" command would be listed as "grep" and not "grep Dash" -- This seems like an easier way.

(The "ps" command has nearly as many options as "ls")

[ Reply to This | # ]

Ignore grep command self-match
Authored by: asher on Aug 05, '05 11:46:19AM

Here's some regular expression info:
http://pegasus.rutgers.edu/~elflord/unix/grep.html
Here's a lot more:
http://www.regular-expressions.info/tutorial.html



[ Reply to This | # ]
Ignore grep command self-match
Authored by: forman on Aug 05, '05 02:25:34PM
To save a little time typing, simply create an "alias" for this command in your "~/.cshrc" file. For instance, I have an alias called "psg" (ps and grep) that omits the original grep from the process list (grep -v grep) and does a case-insensitive search:
alias psg  'ps -augxww | grep -v grep | grep -i'
Add that to your ".cshrc" file, type "source .cshrc" to read in the changes, and then type "psg server" to test it out.

Michael.

[ Reply to This | # ]
Alias for bash users:
Authored by: lionel77 on Aug 05, '05 03:34:10PM

If you are using bash, you want to put the following line into your .bashrc file:

alias psg='ps -augxww | grep -v grep | grep -i'



[ Reply to This | # ]
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 | # ]

Ignore grep command self-match
Authored by: kholburn on Aug 08, '05 01:15:36AM
Here's my preferred ps command. Note that the complicated method for not using grep -v grep doesn't really work here because I like to see the header line so I use egrep to grab that (egrep "PID|$1").

myps () 
{ 
    if [ $# -gt 0 ]; then
        ps -awwwxo "pid ppid %cpu %mem user command" | egrep "PID|$1" | grep -v grep;
    else
        ps -awwwxo "pid ppid %cpu %mem user command";
    fi
}
To use the [m]ethod from sweth's post you would have to say this:

myps () 
{ 
  if [ $# -gt 0 ]; then
    ENDING="${1#?}"
    REST="${1%${ENDING}}"
    ps -awwwxo "pid ppid %cpu %mem user command" | egrep "PID|[$REST]$ENDING"
  else
    ps -awwwxo "pid ppid %cpu %mem user command";
  fi
}

Is it worth it?

[ Reply to This | # ]
Reading Material
Authored by: lullabud on Aug 08, '05 08:42:01PM
When I was first trying to grasp regular expressions I found the O'reilly book to be *very* enlightening. It's not very thick and the information it contains has helped me in perl, php, javascript, and tons of terminal utilities. I highly recommend this book if you're interested in learning Regex syntax.

http://www.oreilly.com/catalog/regex/

[ Reply to This | # ]
Ignore grep command self-match
Authored by: al.cameron on Aug 09, '05 02:56:35PM
Note: the original hint specifically dealt with grep-ing for "literal strings" and then the discussion went off madly in all directions... :-)
However, this did result in a very useful generalization.

gshenaut:
Using the word-boundary is a great modification to allow working with variables. Thanks!

kholburn:
If you combine the REs for the literal string "PID" and the function argument "$1", your function could reduce to:

myps () 
{
    ps -awwwxo "pid ppid %cpu %mem user command" | egrep "[P]ID|\b$1"
}
if you don't mind the extra egrep (which matches all lines) when there is no function argument.

Al

[ Reply to This | # ]

Ignore grep command self-match
Authored by: oo on Mar 22, '06 05:34:35AM

Try this:
ps ax | while read i; do echo $i | grep foobar; done



[ Reply to This | # ]