今まであまり知らずに使ってきたけれど、egrepはgrep -Eのエイリアスだった。
$ man egrep
(なんとgrepのmanが表示される)
(snip)
In addition, the variant programs egrep, fgrep and rgrep are the
same as grep -E, grep -F, and grep -r, respectively. These
variants are deprecated, but are provided for backward
compatibility.
(snip)
grep -E
egrepもといgrep -Eは拡張正規表現(ERE)が使える。
-E, --extended-regexp
Interpret PATTERNS as extended regular expressions (EREs,
see below).
拡張といっても、ふだん正規表現といった場合はたいていEREであるし、場合によってはPCREである、といった具合だと思う。
用途は当然、正規表現によるフィルタリング。
$ curl -v -L https://t.co/1IWhldMN5p 2>&1 | egrep '^> (GET|Host)' > GET /1IWhldMN5p HTTP/2 > Host: t.co > GET /247ABYwk4W HTTP/2 > Host: htn.to > GET /-/redirect?code=77502f725e506b5577504f325e43397d7720393477214f59&location_id=47169081704601537 30&signature=b3a408827bbe742c4b09c55b2ab706695ec65a14cfbba9a3a7ac67f4878d28d7 HTTP/2 > Host: b.hatena.ne.jp > GET /entry/2022/03/14/141720 HTTP/1.1 > Host: mactkg.hateblo.jp
ちなみに何もオプションを付けない素grepではgrep -Gが適用され、標準正規表現(BRE)を使うことになる。
grep -F
fgrepなんて見たことないのだけれど、grep -Fというオプションがある。
-F, --fixed-strings
Interpret PATTERNS as fixed strings, not regular
expressions.
ようするに正規表現を使わずに文字列リテラルとして検索するということらしい。
grep -r
rgrepもといgrep -rはディレクトリを再帰検索するようだ。
-r, --recursive
Read all files under each directory, recursively, following symbolic links only if
they are on the command line. Note that if no file operand is given, grep searches
the working directory. This is equivalent to the -d recurse option.
正直、この用途だとrgを使うと思う。rgは正規表現エンジンの都合で使えない機能があるので、そういうときはrgrep -Pとかを使ってPCREを起動することになりそう。
あわせて読みたい:
おまけ: grep -P
grep -PはPerl正規表現エンジン(PCRE)を使う。おそらく最も強力なマッチングができそう。
BSD grep
BSD grepだと、-Aオプションなどを使うときにちょっとした挙動の違いがあるようだ。
とはいえ、sedほど激しい挙動の違いではないので、あまり困ることはないと思う。不安ならGNU grepを入れると良さそう。