GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • メインページ
  • コミュニティ・ポータル
  • 最近の出来事
  • 新しいページ
  • 最近の更新
  • おまかせ表示
  • 練習用ページ
  • アップロード (ウィキメディア・コモンズ)
  • 特別ページ
  • ヘルプ
  • 利用案内
  • 井戸端
  • お知らせ
  • バグの報告

This view

  • Original page
  • New address

GeezerVison is showing ja.wikipedia.org — grep - Wikipedia

grep(グレップ、グレプ)は、UNIXおよびUnixオペレーティングシステムにおけるコマンドであり、テキストファイルなどプレーンテキスト形式のデータから、指定した正規表現に一致する特定の文字列を含む行を検索して標準出力に書き出す。

概要

grep の名の由来は、ラインエディタedのコマンド g/re/p である。その意味するところは「global regular expression print(ファイル全体から/正規表現に一致する行を/表示する)」で略号になっている[1]。

姉妹コマンドとして、正規表現ではなくリテラル(即値文字列)のみを扱う高速な fgrep[2]、拡張正規表現が使える egrep[3] がある。POSIX では fgrep、egrep を旧形式としていて、それぞれ grep -F、grep -E を使うことを標準としている。Linux Standard Baseでも指定コマンドになっている[4]。


基本構文

grep コマンドの基本的構文は次のとおり。

grep オプション パターン 対象ファイル

対象ファイルは複数指定することもできる。

またパイプ(|)を使い標準入力から検索することもできる。


オブション

オプションには次のようなものがある:

  • -E : 拡張正規表現を使用する。egrep コマンドと同じ動作をする。
  • -F : 正規表現ではなくリテラルを使用する。fgrep コマンドと同じ動作をする。
  • -i : アルファベットの大文字小文字の区別をしない。
  • -o: パターンに一致した箇所のみ出力する。
  • -v : パターンに一致しない行を出力する。
  • -r : 指定したディレクトリおよびその下位ディレクトリ内の全てのファイルを検索する。(「r」はread recursively(再帰的に読む)の略だとも) 次のような構文で使う。
grep -r パターン 検索ディレクトリ

検索ディレクトリ(検索対象ディレクトリ)は相対パスでも絶対パスでも指定できる。たとえば「.」(ドット)と書けば、カレントディレクトリおよびその下位ディレクトリから検索する。

  • -c: 行をそのまま出力するのではなく、パターンに一致した行数のみ出力する。(「c」はcount(カウント。数える)の頭文字)


AND検索やOR検索の場合

AND条件

複数のパターンをAND条件で、すなわち同時に含む行を検索したい場合、パイプ(|)を使いgrepコマンドを連続させる方法がある。 たとえば「abc」という文字列および「xyz」という文字列の両方を含む行を検索したい場合は次のように書く方法がある。

grep abc example.txt | grep xyz
OR条件

複数のパターンをOR条件で、すなわちどちらかのパターンを含む行を検索したい場合はオプション「-e」を使えば良い。たとえば「abc」あるいは「xyz」のどちらかを含む行を検索する場合は次のように書ける。

grep -e abc -e xyz example.txt

移植

テキストから文字列を検索するプログラムとして、Microsoft Windows 用に、有志により、grepWin[5]としてGUI付きで移植された。

参考文献

  • Brian W. Kerninghan、ロブ・パイク『UNIXプログラミング環境』野中浩一訳、石田晴久監訳、アスキー出版局〈ASCII海外ブックス〉、1986年6月。ISBN 4-87148-351-7。 

脚注

  1. ↑ 英: search globally in the file / each lines to which the regular expression matches / print them
  2. ↑ 英: fixed grep または fast grep
  3. ↑ 英: extended grep
  4. ↑ Linux Standard Base https://refspecs.linuxfoundation.org/lsb.shtml
  5. ↑ https://github.com/stefankueng/grepWin

関連項目

  • フィルタ
  • find
  • agrep

外部リンク

  • GNU grep(英語)
  • grep(1) - Linux JMプロジェクトによる日本語のマニュアルページ
  • grep(1) - man page(SunOS リファレンス・マニュアル)
  • grep(1) - man page(HP-UX リファレンス)
スタブアイコン

この項目は、ソフトウェアに関連した書きかけの項目です。この項目を加筆・訂正などしてくださる協力者を求めています(PJ:コンピュータ/P:コンピュータ)。

  • 表示
  • 編集
Unixコマンド
ファイルとファイルシステム管理
  • cat
  • chattr
  • chmod
  • chown
  • chgrp
  • cksum
  • cmp
  • cp
  • dd
  • du
  • df
  • file
  • fsck
  • fuser
  • ln
  • ls
  • lsof
  • mkdir
  • mount
  • mv
  • pax
  • pwd
  • rm
  • rmdir
  • size
  • split
  • tee
  • touch
  • type
  • umask
プロセス管理
  • at
  • bg
  • chroot
  • cron
  • fg
  • kill
  • killall
  • nice
  • pgrep
  • pidof
  • pkill
  • ps
  • pstree
  • reboot
  • shutdown
  • time
  • top
ユーザ管理/環境
  • clear
  • env
  • exit
  • finger
  • history
  • id
  • logname
  • mesg
  • passwd
  • su
  • sudo
  • uptime
  • talk
  • tput
  • uname
  • w
  • wall
  • who
  • whoami
  • write
テキスト処理
  • awk
  • banner
  • basename
  • comm
  • csplit
  • cut
  • diff
  • dirname
  • ed
  • ex
  • fmt
  • fold
  • head
  • iconv
  • join
  • less
  • m4
  • more
  • nkf
  • nl
  • paste
  • patch
  • printf
  • read
  • sed
  • sort
  • spell
  • strings
  • tail
  • tr
  • troff
  • uniq
  • vi
  • wc
  • xargs
  • yes
シェルビルトイン
  • alias
  • cd
  • echo
  • test
  • unset
  • wait
通信
  • dig
  • host
  • ifconfig
  • inetd
  • ip (iproute2)
  • netcat
  • netstat
  • nslookup
  • ping
  • rdate
  • rlogin
  • route
  • ssh
  • traceroute
検索
  • find
  • grep
  • locate
  • whatis
  • whereis
マニュアル
  • apropos
  • help
  • man
ソフトウェア開発
  • ar
  • ctags
  • lex
  • make
  • nm
  • strip
  • yacc
その他
  • bc
  • dc
  • cal
  • date
  • expr
  • false
  • lp
  • lpr
  • od
  • sl
  • sleep
  • stty
  • true
  • tty
  • Category:UNIXのソフトウェア
  • UNIXユーティリティの一覧
「https://ja.wikipedia.org/w/index.php?title=Grep&oldid=110971432」から取得
カテゴリ:
  • 標準UNIXプログラム
隠しカテゴリ:
  • すべてのスタブ記事
  • ソフトウェア関連のスタブ
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.