grepコマンドは、日常の端末操作でもプログラミングでも、Linuxで頻繁に使用されるコマンドです。以下に例を示します。
1.基本的な文法
grep [オプション]パターン[ファイル...]
一般的なパラメータ:
-i:大文字と小文字を区別しません(-iは多くのコマンドでこの意味を持ちます)。
-v、-invert-match:逆表示、一致するテキストを含まないすべての行を表示します。
-R、-r、-recursive:各ディレクトリ内のすべてのファイルを再帰的に読み取ります。-drecurseオプションと同等です。
-o、-only-matching:一致した行のPATTERNに一致する部分のみを表示します。
-n、-line-number:各出力行の前にあるファイルに行番号を追加します。
--color:一致したコンテンツを強調表示します。このパラメーターは通常、デフォルトでgrepに追加されます。
-A NUM、-after-context = NUM:一致した行の直後に次のNUM行を出力します。
-B NUM、-before-context = NUM:一致した行の前に上記のNUM行を出力します。
-C NUM、-context = NUM:一致した行のコンテキストの前後のNUM行を出力します。
2.例
2.1パラメータなし
以下に示すように、現在のディレクトリで、文字列「hosts」を含むファイルを見つけます。
[root@localhost ssh]# grep "hosts" ./*
./ssh_config:# RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#
検索結果では、ファイルが最初にリストされ、次に文字列「hosts」が配置されている行が出力されます。デフォルトでは、ここの「ホスト」など、一致したコンテンツが強調表示されます。
以下に示すように、システム内のコマンドのエイリアスを確認します。
[root@localhost ssh]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ssh]#
その中で、コマンドgrepはgrep --color = autoと同じであり、-colorパラメーターがデフォルトで追加されています。
2.2-rパラメーター
現在のディレクトリで、文字列「hosts」を含むファイルを再帰的に検索します。つまり、現在のディレクトリと現在のディレクトリの下にあるすべてのディレクトリを検索し、再帰的に検索します。次のように:
[root@localhost etc]# grep -r "hosts" ./*
./avahi/hosts:# See avahi.hosts(5) for more information on this configuration file!
./cupshelpers/preferreddrivers.xml: <drivertype name="ghostscript">
./cupshelpers/preferreddrivers.xml: <attribute name="ppd-product" match=".*Ghostscript"/>
./cupshelpers/preferreddrivers.xml: <drivertype>ghostscript</drivertype>
./dnsmasq.conf:# from /etc/hosts or DHCP only.
./dnsmasq.conf:# If you don't want dnsmasq to read /etc/hosts, uncomment the
./dnsmasq.conf:#no-hosts
./dnsmasq.conf:# or if you want it to read another file, as well as /etc/hosts, use
./dnsmasq.conf:#addn-hosts=/etc/banner_add_hosts
./dnsmasq.conf:# automatically added to simple names in a hosts-file.
./dnsmasq.conf:#expand-hosts
……
./tcsd.conf:# on this machine's TCSD by TSP's on non-local hosts (over the internet).
./yum/pluginconf.d/fastestmirror.conf:hostfilepath=timedhosts.txt
[root@localhost etc]#
2.3-iパラメーター
以下に示すように、現在のディレクトリで文字列「hosts」を含むファイルを見つけます。文字列内の文字では大文字と小文字が区別されません。
[root@localhost ssh]# grep -i "hosts" ./*
./ssh_config:# HOSTS tmp
./ssh_config:# RhostsRSAAuthentication no
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#
上記の検索結果では、HOSTS、Hosts、およびhostsがすべて見つかりました。
2.4-oパラメーター
現在のディレクトリで文字列「hosts」を含むファイルを見つけ、「hosts」と同じコンテンツのみを表示します。次のように:
[root@localhost ssh]# grep -o "hosts" ./*
./ssh_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
./sshd_config:hosts
[root@localhost ssh]#
その中で、表示される一致するコンテンツは、「ホスト」と同じコンテンツのみを表示します。
2.5-nパラメータ
以下に示すように、現在のディレクトリで文字列「hosts」を含むファイルを見つけ、一致するコンテンツの行番号を表示します。
[root@localhost ssh]# grep -n "hosts" ./*
./ssh_config:24:# RhostsRSAAuthentication no
./sshd_config:54:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config:56:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config:59:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:60:#IgnoreRhosts yes
[root@localhost ssh]#
行番号は試合前に追加されます。
2.6 -A、-B、-Cパラメーター
-A:以下に示すように、一致した行の直後に次の1行を印刷します。
[root@localhost ssh]# grep -A 1 "hosts" ./*
./ssh_config:# RhostsRSAAuthentication no
./ssh_config-# RSAAuthentication yes
--
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
--
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
[root@localhost ssh]#
その中で、「-」は、隣接する一致するグループの間に-の行が出力されることを意味します。2つの一致するグループが実際のファイルで密接に接続されている場合、「-」の区切りはありません。
-B:以下に示すように、一致した行の前に上記の2行を印刷します。
[root@localhost ssh]# grep -B 2 "hosts" ./*
./ssh_config-# ForwardAgent no
./ssh_config-# ForwardX11 no
./ssh_config:# RhostsRSAAuthentication no
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
[root@localhost ssh]#
-C:以下に示すように、一致した行のコンテキストの前後に2行を印刷します。
[root@localhost ssh]# grep -C 2 "hosts" ./*
./ssh_config-# ForwardAgent no
./ssh_config-# ForwardX11 no
./ssh_config:# RhostsRSAAuthentication no
./ssh_config-# RSAAuthentication yes
./ssh_config-# PasswordAuthentication yes
--
./sshd_config-#AuthorizedKeysCommandUser nobody
./sshd_config-
./sshd_config:# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
./sshd_config-#HostbasedAuthentication no
./sshd_config:# Change to yes if you don't trust ~/.ssh/known_hosts for
./sshd_config-# HostbasedAuthentication
./sshd_config-#IgnoreUserKnownHosts no
./sshd_config:# Don't read the user's ~/.rhosts and ~/.shosts files
./sshd_config:#IgnoreRhosts yes
./sshd_config-
./sshd_config-# To disable tunneled clear text passwords, change to no here!
[root@localhost ssh]#
3、まとめ
grepは、Linuxターミナル操作でよく使用されるコマンドです。通常、指定されたコンテンツが含まれているファイルを見つけるために使用されます。
参照:
[1] Linuxgrepマニュアル。