Log-Files
Grep 查找從模式 A 開始的行,直到匹配模式 B
我有一個包含如下位的日誌:
[2012-04-16 15:16:43,827: DEBUG/PoolWorker-2] {'feed': {}, 'bozo': 1, 'bozo_exception': URLError(error(110, 'Connection timed out'),), 'entries': []} [2012-04-16 15:16:43,827: ERROR/PoolWorker-2] get_entries Traceback (most recent call last): File "/opt/myapp/app.py", line 491, in get_entries logging.getLogger(__name__).debug("Title: %s" % doc.title) File "build/bdist.linux-x86_64/egg/feedparser.py", line 423, in __getattr__ raise AttributeError, "object has no attribute '%s'" % key AttributeError: object has no attribute 'title' [2012-04-16 15:16:43,828: INFO/MainProcess] Task myapp.do_task[4fe968ff-e069-4cfe-9a81-aece0d97c289] succeeded in 21.0481028557s: None
我想從中提取如下部分:
- 當一行包含“ERROR”或“WARN”時開始過濾(並包含此行)
- 當找到以“[”開頭的下一行時,停止過濾(並且不要包含此行)。
我很確定這對 Grep 來說太過分了,那麼該怎麼做呢?
(好吧,我不是懶惰,而是想通了 - 將發布我的解決方案。)
這對我有用 - 不完全如上所述,但足夠接近:
awk '/ERROR|WARN/,/DEBUG|INFO/ { if ($0 !~ /(DEBUG|INFO)/) { print } }' < logfile
awk 支持這個非常方便:
/startpattern/,/stoppattern/ { }
. 不幸的是,如果停止模式與開始模式在同一行匹配,它只會列印出該行,因此需要不同的停止模式。
嘗試這樣的事情:
cat importantstuff.log | grep 'File .*, line .*, in .*' -B 1 -A 2
沒有完全回答這個問題,但我認為它完成了任務。
在匹配之後或之前用於 grep 控制上下文行的
-A
and標誌。-B
這是有效的,因為 grep 將相鄰的匹配分組,因此您最終會得到很好的分離回溯:
Traceback (most recent call last): File "sfquest.py", line 9, in b c() File "sfquest.py", line 15, in c d() File "sfquest.py", line 20, in d raise Exception('important information') Exception: important information -- Traceback (most recent call last): File "sfquest.py", line 9, in b c() File "sfquest.py", line 15, in c d() File "sfquest.py", line 20, in d raise Exception('important information') Exception: important information -- Traceback (most recent call last): File "sfquest.py", line 9, in b c() File "sfquest.py", line 15, in c d() File "sfquest.py", line 20, in d raise Exception('important information') Exception: important information
這是我用來生成回溯範例的範常式式碼:
import traceback def a(): b() def b(): for i in range(10): try: c() except Exception, e: print 'bad stuff' print traceback.format_exc(e) def c(): d() def d(): for i in range(10): print 'random junk' raise Exception('important information') a()