20
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

リアルタイムでログを複数条件grepする (tailfを複数grep的な?)

Posted at

こんにちわ。

皆さん、リアルタイムでログを見てる時、何か条件で結果を絞りたい場合どうしますか?

多分こうすると思います。

tail -f hoge.log | grep "test"

これで test で絞ったログがダダダダダーってリアルタイムで見れる事が出来ます。

じゃあ、 test で絞りたいんだけど、png の画像データへのログは無視したい場合、どうしますか?

tail -f hoge.log | grep "test" | grep -v ".png"

僕はこうしてみました。
が、結果は・・・

暫く経過して表示されました・・・・

リアルタイムじゃない!!

原因

これは、grepが勝手に結果をバッファしてしまっている為に、暫く経ってから出力されるのでした。。

結論

ここまで引っ張って・・という感じもありますが、じゃあどうすれば複数の検索結果をリアルタイムで出せるのかという話ですが、、、

結論、grepにバッファさせなければOKです。
grepに --line-buffered というオプションがありますのでこれを使います。

正解としては、下記のようにすればリアルタイムで確認刷ることが出来るようになります。
めでたしめでたし。

tail -f hoge.log | grep --line-buffered "test" | grep -v --line-buffered ".png"
20
17
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
20
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?