Loose-Info.com
Last Update 2014/03/11
TOP - Unix系OS - FreeBSD - grep(1)

パターンマッチする行を表示します。

grep オプション1 パターン1 ファイルパス1 grep オプション1 -e パターン1 ファイルパス1 grep オプション1 -f ファイルパス2 ファイルパス1
オプション1(任意) -A 値1 マッチした行に続く(値1)行分を付加して出力 -B 値1 マッチした行の前の(値1)行分を付加して出力 -C 値1 マッチした行の前、および続く(値1)行分を付加して出力 -c マッチした行数を表示 -e パターン2 (パターン2)をパターンとして指定 「-」で始まるパターンを指定可能 -f ファイルパス3 (ファイルパス3)で検索パターンのファイルを指定 行単位でマッチを確認 -H マッチした行のファイル名も表示 -i 大文字、小文字の区別をしない -L マッチしないファイル名の出力 マッチ行の出力は無し -l マッチしたファイル名の出力 マッチ行の出力は無し -n 行の出力の際に行番号を付加 -o マッチした部分のみを出力 -R ディレクトリ内の全てのファイルを再帰的に読み込み -s エラーメッセージを抑止 -v マッチしない行を出力 -w ワードに完全にマッチする行を出力 -x 行全体に完全にマッチする行を出力 パターン1 検索に使用するパターン ファイルパス1 検索の対象となるファイル ファイルパス2 検索パターンを含むファイル

※ オプションなどは個人的に重要と考えられるものを記述しており、記載の無いものは、manページや関連書籍などを参照願います。
※ 実行例の記述は、不要と考えられる部分の削除などの修正を行ったものを掲載しています。
※ 実行例の実行環境はFreeBSD 9.1です。

(例) オプション無し
実行結果
$ grep character /usr/include/stdio.h * NB: to fit things in six character monocase externals, the stdio * by a three-character attempt at a mnemonic. * _ub, _up, and _ur are used when ungetc() pushes back more characters * than fit in the current _bf, or when ungetc() pushes back a character

(例) -A
実行結果
$ grep -A 2 character /usr/include/stdio.h * NB: to fit things in six character monocase externals, the stdio * code uses the prefix `__s' for stdio objects, typically followed * by a three-character attempt at a mnemonic. */ -- * _ub, _up, and _ur are used when ungetc() pushes back more characters * than fit in the current _bf, or when ungetc() pushes back a character * that does not match the previous one in _bf. When this happens, * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff

(例) -B
実行結果
$ grep -B 2 character /usr/include/stdio.h /* * NB: to fit things in six character monocase externals, the stdio * code uses the prefix `__s' for stdio objects, typically followed * by a three-character attempt at a mnemonic. -- * code as compact as possible. * * _ub, _up, and _ur are used when ungetc() pushes back more characters * than fit in the current _bf, or when ungetc() pushes back a character

(例) -C
実行結果
$ grep -C 2 character /usr/include/stdio.h /* * NB: to fit things in six character monocase externals, the stdio * code uses the prefix `__s' for stdio objects, typically followed * by a three-character attempt at a mnemonic. */ -- * code as compact as possible. * * _ub, _up, and _ur are used when ungetc() pushes back more characters * than fit in the current _bf, or when ungetc() pushes back a character * that does not match the previous one in _bf. When this happens, * _ub._base becomes non-nil (i.e., a stream has ungetc() data iff

(例) -e
実行結果
$ grep -e -1 /usr/include/stdio.h short _file; /* (*) fileno, if Unix descriptor, else -1 */ #define EOF (-1) * See ISO/IEC 9945-1 ANSI/IEEE Std 1003.1 Second Edition 1996-07-12

(例) -f
実行結果
$ cat grep_ptn.txt character GNU $ grep -f grep_ptn.txt /usr/include/stdio.h * NB: to fit things in six character monocase externals, the stdio * by a three-character attempt at a mnemonic. * _ub, _up, and _ur are used when ungetc() pushes back more characters * than fit in the current _bf, or when ungetc() pushes back a character * c) the app defines a GNUism such as _BSD_SOURCE or _GNU_SOURCE #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE) #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE) #if defined(__GNUC__) && defined(__STDC__)

(例) -H
実行結果
$ grep -H character /usr/include/stdio.h /usr/include/stdio.h: * NB: to fit things in six character monocase externals, the stdio /usr/include/stdio.h: * by a three-character attempt at a mnemonic. /usr/include/stdio.h: * _ub, _up, and _ur are used when ungetc() pushes back more characters /usr/include/stdio.h: * than fit in the current _bf, or when ungetc() pushes back a character

(例) -i
実行結果
$ grep -i gnu /usr/include/stdio.h * c) the app defines a GNUism such as _BSD_SOURCE or _GNU_SOURCE #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE) #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE) #if defined(__GNUC__) && defined(__STDC__)

(例) -L
実行結果
$ grep -L stdlib /usr/include/stdio.h /usr/include/stdlib.h /usr/include/stdio.h

(例) -l
実行結果
$ grep -l stdlib /usr/include/stdio.h /usr/include/stdlib.h /usr/include/stdlib.h

(例) -n
実行結果
$ grep -n character /usr/include/stdio.h 71: * NB: to fit things in six character monocase externals, the stdio 73: * by a three-character attempt at a mnemonic. 100: * _ub, _up, and _ur are used when ungetc() pushes back more characters 101: * than fit in the current _bf, or when ungetc() pushes back a character

(例) -o
実行結果
$ grep -n -o character /usr/include/stdio.h 71:character 73:character 100:character 101:character

(例) -R
実行結果
$ grep -R -l character /usr/include/net /usr/include/net/ppp_defs.h /usr/include/net/zlib.h

(例) -s
実行結果
$ grep character /usr/include/stdio.hh grep: /usr/include/stdio.hh: No such file or directory $ grep -s character /usr/include/stdio.hh

(例) -v
実行結果
$ grep -n -v character /usr/include/stdio.h : : 68:#define _FSTDIO /* Define for new stdio with functions. */ 69: 70:/* 72: * code uses the prefix `__s' for stdio objects, typically followed 74: */ 75: 76:/* stdio buffers */ 77:struct __sbuf { : :

(例) -w
実行結果
$ grep -w BSD /usr/include/stdio.h #endif /* BSD or X/Open before issue 6 */

(例) -x
実行結果
$ grep -n -x "#endif" /usr/include/stdio.h 48:#endif 54:#endif 58:#endif 59:#endif 65:#endif 66:#endif 151:#endif 159:#endif 203:#endif 209:#endif 215:#endif 218:#endif 221:#endif 230:#endif : :