「Grep」の版間の差分

提供: Wikinote
移動: 案内検索
(and/or 検索)
(Tips)
行1: 行1:
 
== Tips ==
 
== Tips ==
 +
 +
=== 激速!! 再帰的パラレル grep ===
 +
ディレクトリ以下のファイルを再帰的に検索する場合、普通は以下のように -r オプションを使用する。
 +
$ grep -r PATTERN TARGET_DIR
 +
 +
CPU コアを複数搭載している PC の場合、xargs の -P オプションを使用して、grep を複数起動すると検索が高速になる。
 +
[4 コアの場合]
 +
$ find TARGET_DIR | xargs -P 4 grep PATTERN
  
 
=== and/or 検索 ===
 
=== and/or 検索 ===

2010年3月24日 (水) 00:35時点における版

Tips

激速!! 再帰的パラレル grep

ディレクトリ以下のファイルを再帰的に検索する場合、普通は以下のように -r オプションを使用する。

$ grep -r PATTERN TARGET_DIR

CPU コアを複数搭載している PC の場合、xargs の -P オプションを使用して、grep を複数起動すると検索が高速になる。

[4 コアの場合]
$ find TARGET_DIR | xargs -P 4 grep PATTERN

and/or 検索

and 検索をしたい場合は、grep に 2 度かける。

$ cat hoge.txt | grep hoge | grep fuga

順序を考慮した and 検索をしたい場合は、正規表現を使う。

$ cat hoge.txt | grep "hoge.*fuga"

or 検索をしたい場合は、-e オプションでパタンを羅列すれば良い。

$ ifconfig | grep -e "^\w" -e inet

あるいは、egrep を使う。

$ ifconfig | egrep "eth|lo"

検索文字列をハイライト表示

.bashrc に以下を追加しておく。

export GREP_COLOR='1;37;41'
alias grep='grep --color=auto'

ファイル名付き cat

例えば、/proc や /sys 中の情報を一覧表示したい場合、単なる cat だと、何がなんだかわからない。

$ cat /proc/sys/net/core/*
64
10
5
300
1000
 :

こういうときは、grep で空文字を検索すればよい。

$ grep "" /proc/sys/net/core/*
/proc/sys/net/core/dev_weight:64
/proc/sys/net/core/message_burst:10
/proc/sys/net/core/message_cost:5
/proc/sys/net/core/netdev_budget:300
/proc/sys/net/core/netdev_max_backlog:1000
 :

また、grep ではないけれど、head や tail を使うと、ファイル名がタイトルっぽく表示される。

$ head /proc/sys/net/core/*
==> /proc/sys/net/core/dev_weight <==
64

==> /proc/sys/net/core/message_burst <==
10

==> /proc/sys/net/core/message_cost <==
5
: