關于Grep的多次管道過濾的問題及解決
Grep的多次管道過濾問題
在日常的開發(fā)過程中,我們利用grep可以方便快捷的查找感興趣的日志內(nèi)容,極大地提升了開發(fā)和排錯效率。但是有時候,我們也會遇到一些問題,比如。
- crazy.log 是某個進程不斷輸出日志的文件
- 我們使用tail -f crazy.log來檢測日志的產(chǎn)生
- 我們在前面的基礎上利用管道增加一層過濾篩選感興趣的內(nèi)容。
tail -f crazy.log | grep Hello Hello,printting from Ruby Hello,Time is 1566096393 Hello,printting from Ruby Hello,Time is 1566096393 Hello,printting from Ruby Hello,Time is 1566096393 Hello,printting from Ruby Hello,Time is 1566096393 Hello,printting from Ruby Hello,Time is 1566096393
那么當我們再次增加一個過濾是,卻沒有內(nèi)容(立即)產(chǎn)生了
? /tmp tail -f crazy.log | grep Hello | grep Time
如何解決
tail -f crazy.log | grep --line-buffered Hello | grep Time Hello,Time is 1566096393 Hello,Time is 1566096393 Hello,Time is 1566096393 Hello,Time is 1566096393 Hello,Time is 1566096393
如上,我們使用grep的選項--line-buffered即可。
line-buffered 是什么
--line-buffered
Force output to be line buffered. By default, output is line buffered when standard output is
a terminal and block buffered otherwise.
上面的意思是
- 強制輸出結(jié)果使用行緩沖
- 默認情況下,如果標準輸入時終端,則使用line bufferred
- 否則,使用塊緩沖,(默認的大小為4096 bytes,因系統(tǒng)和配置而異)
所以,這也就解釋了為什么雙重grep過濾沒有內(nèi)容,因為沒有達到塊緩沖限制。
以上。
總結(jié)
這些僅為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot + Vue 項目部署上線到Linux 服務器的教程詳解
這篇文章主要介紹了SpringBoot + Vue 項目部署上線到Linux 服務器,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
Linux+php+apache+oracle環(huán)境搭建之CentOS下安裝Oracle數(shù)據(jù)庫
研究了兩天Linux下安裝Oracle,重裝了兩次虛擬機,終于安裝成功。很有收獲的。記錄下安裝過程。大神們?nèi)缬懈玫姆绞?,請?lián)系我!2014-08-08

