LoginSignup
7
9

More than 5 years have passed since last update.

grep での 正規表現、 プロセスを見るときなどに使うコマンド

Last updated at Posted at 2013-09-13

プロセスを見るときなど、grep して対象の文字列を除いて検索したりすることがよくあります。
いつもはこれを使ってます。

ps aux | grep mysql | grep -v grep

でもこんな方法もあるって知りました。

ps aux | grep \[h\]ttpd

はほぼおなじ意味合いを持っているのかな?

正規表現のだと、

[defg]ebu

↑これはdebu, eebu,febu,gebu とマッチします。

grep \[h\]ttpd

↑これは、エスケープを省くと下の様になります。

grep [h]ttpd

これは httpd という文字列を検索することになるのですが、grep のコマンドは grep [h]ttpd なので、grep に引っかかることはありません。
ややこしいですね(´;ω;`)

$ ps aux | grep httpd
hoge      9994  0.0  0.0 5539  1234 ?        Ss   Mar19  46:22 /fuga/hoge/httpd -k start
hoge      689  0.0  0.0 5539  1111 ?        S    12:08   0:00 /fuga/hoge/httpd -k start
watashi   689  0.0  0.0 7452   987 pts/6    S+   12:11   0:00 grep httpd
$ ps aux | grep httpd | grep -v grep
hoge      9994  0.0  0.0 5539  1234 ?        Ss   Mar19  46:22 /fuga/hoge/httpd -k start
hoge      689  0.0  0.0 5539  1111 ?        S    12:08   0:00 /fuga/hoge/httpd -k start
$ ps aux | grep \[h\]ttpd  検索コマンドの対象文字列と実際の検索対象文字列は異なる(・∀・)
hoge      9994  0.0  0.0 5539  1234 ?        Ss   Mar19  46:22 /fuga/hoge/httpd -k start
hoge      689  0.0  0.0 5539  1111 ?        S    12:08   0:00 /fuga/hoge/httpd -k start

実際の文字列は以下の様になるので、そりゃヒットはしないですね。

watashi   689  0.0  0.0 7452   987 pts/6    S+   12:11   0:00 grep [h]ttpd

簡単な気もするし難しい気もしますね。

[参考]
http://yoshiki.github.io/blog/2008/02/02/ps-auxww-grep-h/
http://www.ocn.ne.jp/hosting/support/manual/mwpro2/hp02/06_01.html

7
9
9

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9