Section Next | Prev


5.2.1 文字列検索

UNIX系OSに実装されているツールで、最も強力で豊富な機能を提供している物のひとつに 文字列検索があります。
特に「正規表現 : regular expression」は複雑な文字列の組み合わせを矛盾無く表現できる方法として UNIX以外にも取り込まれています。 その正規表現を使ってファイルの中から特定の文字列を含む行を表示するツールが grep (global regular expression printer) になります。

grep の簡単な使い方

【機能説明】
grep はファイルの中を検索し、指定された文字列を含む行を表示します。 文字列には正規表現が用いられます。 fgrep は単純な文字列検索しかできませんが、高速な検索が可能です。 また egrep は完全正規表現が利用できるため、複雑な検索が可能です。

【入力フォーマット】
grep 文字列 [ファイル名 ... ]
fgrep 文字列 [ファイル名 ... ]
egrep 文字列 [ファイル名 ... ]

【主なオプション】
-c (count)
一致した行数を表示
--color
一致した箇所を色を変えて表示
-i (ignore case)
英大文字/小文字の区別をしない
-l (list file name)
ファイル名の表示
-n (number)
行番号の表示
-v (invert)
文字列を含まない行を表示
-w (word)
文字列を単語として区別する

【実行例】
ファイル一覧から、特定のファイル名を表示。
	$ ls
	DECSTATION              lat.txt                 programming2.doc
	README                  osf_user.doc            sort.txt
	decwrite_text.doc.Z     osf_user.doc_backup
	$ ls | grep ^[A-Z]	# 大文字で始まるファイル一覧
	DECSTATION
	README
	$ ls |grep -v doc	# "doc" を含まないファイル一覧
	DECSTATION
	README
	lat.txt
	sort.txt
文字列 over を含む行を表示
	$ grep --color over grep.txt # 単に over を検索
	      correctly).   Specifying  -U  overrules this guesswork, causing
	Repetition  takes  precedence  over concatenation, which in turn takes
	precedence over alternation.  A whole subexpression may be enclosed in
	parentheses to override these precedence rules.

	$ grep --color -w over grep.txt	# 単語としての over を検索
	Repetition  takes  precedence  over concatenation, which in turn takes
	precedence over alternation.  A whole subexpression may be enclosed in

文字列 over を含む、ファイルの一覧
	$ grep -wl over /usr/include/*.h
	/usr/include/argp.h
	/usr/include/db.h
	/usr/include/edsio.h
	/usr/include/features.h
	/usr/include/gmpxx.h
	/usr/include/gpm.h
	/usr/include/hpojip.h
	/usr/include/idn-free.h
		:

正規表現のまとめ
文字を表現するためのキーワード(特殊な文字)はワイルドカードとか、メタキャラクタと呼ばれます。 grep系ツールで利用できる正規表現の代表的なメタキャラクタは以下の通りです。
メタキャラクタ意味FGE
. (ドット)任意の一文字 a. は aa, a1 などを表す-**
^行の先頭^# はシャープで始まる行-**
$行の末尾.$ はドットで終わる行-**
[xyz]x,y,z のいづれか[a-z] は英小文字-**
[^xyz]x,y,z を含まない文字の集合[^A-Z] は英大文字以外-**
*直前の文字列が0回以上繰り返すe[xy]* は e, ex, ey など--*
+直前の文字列が1回以上繰り返すe[xy]+ は ex, ey など--*
(xx|yy)xx または yy(pop3|imap4) は pop3 か imap4--*
FGE = fgrep, grep, egrep で利用できるものは * 、利用できないものは - で表現。
例えば [] の表現は fgrep 以外で利用可能

実行例1
先頭が # で始まる行に冠する grep の例
# コメント行のみを抽出
$ grep ^# /etc/httpd/conf/httpd.conf | head
#
# Based upon the NCSA server configuration files originally by Rob McCool.
#
# This is the main Apache server configuration file.  It contains the
# configuration directives that give the server its instructions.
# See  for detailed information about
# the directives.
#
# Do NOT simply read the instructions in here without understanding
# what they do.  They're here only as hints or reminders.  If you are unsure

# コメント以外を抽出(上記の反対)
$ grep -v ^# /etc/httpd/conf/httpd.conf | head


ServerTokens Full

ServerRoot "/etc/httpd"

PidFile run/httpd.pid

Timeout 120

# コメント、空行以外([xx]は空白を含まない点に注意
$ grep ^[^#] /etc/httpd/conf/httpd.conf | head
ServerTokens Full
ServerRoot "/etc/httpd"
PidFile run/httpd.pid
Timeout 120
KeepAlive Off
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule prefork.c>
StartServers       8
MinSpareServers    5
完全正規表現の例 (egrep)
$ netstat -at |egrep "(pop3|smtp)"
tcp        0      0 *:pop3                      *:*                         LISTEN
tcp        0      0 *:smtp                      *:*                         LISTEN
tcp        0      0 site36.atw.ne.jp:http       smtp.roissy-online.co:44855 TIME_WAIT

Section Next | Prev

Copyright 2007-2018 ycosSystems Misc/Body521.html