最終更新: engineernochiebukuro 2009年12月15日(火) 23:14:36履歴
GNU grep には様々なオプションがあるが、以下に便利だと思われるオプションの例を挙げる。
GNU grep の詳細は以下のページを参照のこと。
http://www.gnu.org/software/grep/doc/grep.html#SEC...
以下のようなファイルを例にする。
まずは基本的なところから。
複数の検索パターンを指定する場合は以下のようにする。
もしくは次のようにegrepを使う。
以下は色々な正規表現を使った例。
もしくは、
あるいは、
GNU grep の詳細は以下のページを参照のこと。
http://www.gnu.org/software/grep/doc/grep.html#SEC...
以下のようなファイルを例にする。
$ cat aaa.txt abcdefg hijklmn opqrstu vwzyz test 12345 67890 ABCDEFG HIJKLMN OPQRSTU VWZYZ abctestdef 123testxyz aiueo kakikukeko sashisuseso irohanihoheto
まずは基本的なところから。
$ grep -v abc aaa.txt hijklmn opqrstu vwzyz test 12345 67890 ABCDEFG HIJKLMN OPQRSTU VWZYZ 123testxyz aiueo kakikukeko sashisuseso irohanihoheto
パターンに一致したときは、終了ステータスが0。一致しないときは、1になる。
$ grep -q abc aaa.txt $ echo $? 0 $ grep -q ZZZ aaa.txt $ echo $? 1
$ grep -C 1 ABC aaa.txt 67890 ABCDEFG HIJKLMN
複数の検索パターンを指定する場合は以下のようにする。
$ grep -e 123 -e abc aaa.txt abcdefg 12345 abctestdef 123testxyz
もしくは次のようにegrepを使う。
$ egrep "123|abc" aaa.txt abcdefg 12345 abctestdef 123testxyz
以下は色々な正規表現を使った例。
$ grep '[0123456789]' aaa.txt 12345 67890 123testxyz
もしくは、
$ grep '[0-9]' aaa.txt 12345 67890 123testxyz
あるいは、
$ grep '[[:digit:]]' aaa.txt 12345 67890 123testxyz
$ grep '[^0-9]' aaa.txt abcdefg hijklmn opqrstu vwzyz test ABCDEFG HIJKLMN OPQRSTU VWZYZ abctestdef 123testxyz aiueo kakikukeko sashisuseso irohanihoheto
$ grep 'te.t' aaa.txt test abctestdef 123testxyz
$ grep 'A.*G' aaa.txt ABCDEFG
$ grep --help Usage: grep [OPTION]... PATTERN [FILE]... Search for PATTERN in each FILE or standard input. PATTERN is, by default, a basic regular expression (BRE). Example: grep -i 'hello world' menu.h main.c Regexp selection and interpretation: -E, --extended-regexp PATTERN is an extended regular expression (ERE) -F, --fixed-strings PATTERN is a set of newline-separated fixed strings -G, --basic-regexp PATTERN is a basic regular expression (BRE) -P, --perl-regexp PATTERN is a Perl regular expression -e, --regexp=PATTERN use PATTERN for matching -f, --file=FILE obtain PATTERN from FILE -i, --ignore-case ignore case distinctions -w, --word-regexp force PATTERN to match only whole words -x, --line-regexp force PATTERN to match only whole lines -z, --null-data a data line ends in 0 byte, not newline Miscellaneous: -s, --no-messages suppress error messages -v, --invert-match select non-matching lines -V, --version print version information and exit --help display this help and exit --mmap use memory-mapped input if possible Output control: -m, --max-count=NUM stop after NUM matches -b, --byte-offset print the byte offset with output lines -n, --line-number print line number with output lines --line-buffered flush output on every line -H, --with-filename print the filename for each match -h, --no-filename suppress the prefixing filename on output --label=LABEL print LABEL as filename for standard input -o, --only-matching show only the part of a line matching PATTERN -q, --quiet, --silent suppress all normal output --binary-files=TYPE assume that binary files are TYPE; TYPE is `binary', `text', or `without-match' -a, --text equivalent to --binary-files=text -I equivalent to --binary-files=without-match -d, --directories=ACTION how to handle directories; ACTION is `read', `recurse', or `skip' -D, --devices=ACTION how to handle devices, FIFOs and sockets; ACTION is `read' or `skip' -R, -r, --recursive equivalent to --directories=recurse --include=FILE_PATTERN search only files that match FILE_PATTERN --exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN --exclude-from=FILE skip files matching any file pattern from FILE --exclude-dir=PATTERN directories that match PATTERN will be skipped. -L, --files-without-match print only names of FILEs containing no match -l, --files-with-matches print only names of FILEs containing matches -c, --count print only a count of matching lines per FILE -T, --initial-tab make tabs line up (if needed) -Z, --null print 0 byte after FILE name Context control: -B, --before-context=NUM print NUM lines of leading context -A, --after-context=NUM print NUM lines of trailing context -C, --context=NUM print NUM lines of output context -NUM same as --context=NUM --color[=WHEN], --colour[=WHEN] use markers to highlight the matching strings; WHEN is `always', `never', or `auto' -U, --binary do not strip CR characters at EOL (MSDOS) -u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS) `egrep' means `grep -E'. `fgrep' means `grep -F'. Direct invocation as either `egrep' or `fgrep' is deprecated. With no FILE, or when FILE is -, read standard input. If less than two FILEs are given, assume -h. Exit status is 0 if any line was selected, 1 otherwise; if any error occurs and -q was not given, the exit status is 2. Report bugs to <bug-grep@gnu.org>.
タグ
このページへのコメント
Business networking
Banking, mortgage
topimvse.ru
5uz71b Thanks for the blog post.Really thank you! Will read on...
X9gM1n I think this is a real great post.Much thanks again. Will read on...
grep - 鐃緒申鐃緒申鐃出ワ申鐃藷ジニワ申鐃緒申鐃塾件申鐃緒申 - Seesaa Wiki鐃淑ワ申鐃緒申鐃緒申鐃緒申 for 鐃緒申鐃殉¥申鐃夙フワ申鐃緒申 ??≪?潟?壔???若?? ??泣?ゃ??2 http://www.pslcbi.com/moncler2014.html
grep - 鐃緒申鐃緒申鐃出ワ申鐃藷ジニワ申鐃緒申鐃塾件申鐃緒申 - Seesaa Wiki鐃淑ワ申鐃緒申鐃緒申鐃緒申 for 鐃緒申鐃殉¥申鐃夙フワ申鐃緒申 ??≪?潟?壔???若?? ??鴻?若????若?潟????? http://www.pslcbi.com/moncler2014.html