LoginSignup
14
14

More than 5 years have passed since last update.

pcregrepで複数行の文字列をマッチさせる

Posted at

複数行にまたがる文字列をgrepで検索したいときには、
perlの正規表現が使えるpcregrepが便利です。

インストール

CentOS6には標準でインストールされていますが、
CentOS7やAmazonLinuxの場合は、
pcregrepが含まれるpcre-toolsパッケージをインストールしてください。

yum install pcre-tools

使い方

例えば以下のようなファイルがあるときに

test.txt
abc
hoge
123

pcregrepをMオプション付きで実行すると...

$ pcregrep -M "abc\nhog"  test.txt
abc
hoge
$ pcregrep -v -M "a.*\n.*g"  test.txt
123

改行を含んだ文字列をマッチさせることができます。
複数行で出力されるログの監視などで重宝しています!

man page

14
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14