17 January 2010

grep [s]tring anyone?

This is another grep's adventure. It started from a thought: "is there a simple way so that when I grep for something, let's say ps output, grep itself isn't shown there?"

My usual recipe is like below:

$ ps auxw | grep httpd | grep -v grep

But recently I found a neat trick:

$ ps auxw | grep [h]ttpd

Why is that working? Imagine that in the previous command , "ps auxw" would output these two lines among others:

mulyadi 30632 0.0 0.0 5852 1516 pts/8 Ss+ Jan09 0:00 httpd
mulyadi 30950 0.0 0.0 5852 1420 pts/9 Ss+ Jan07 0:00 grep httpd 

When you find for httpd, of course both of them will be caught because both of them has "httpd" string. The situation change for the 2nd command:

mulyadi 30632 0.0 0.0 5852 1516 pts/8 Ss+ Jan09 0:00 httpd
mulyadi 30950 0.0 0.0 5852 1420 pts/9 Ss+ Jan07 0:00 grep [h]ttpd

'What, how come?" you say? Recall you invoke "grep [h]ttpd", right? So, that's what displayed in ps. But grep interpret this regular expression differently. It means "I search for httpd", not "[h]ttpd". Everything enclosed in [] is something to search for, unless the "[" or "]" is escaped.

Clever trick I would say.... took me a while to understand it.

How to execute multiple commands directly as ssh argument?

 Perhaps sometimes you need to do this: ssh user@10.1.2.3 ls It is easy understand the above: run ls after getting into 10.1.2.3 via ssh. Pi...