我有一个包含如下内容的日志:
[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 来说太多了,那么该怎么办呢?
(好吧,我没有偷懒,我已经找到方法了——我会发布我的解决方案。)
答案1
这对我有用 - 不完全像上面描述的,但足够接近:
awk '/ERROR|WARN/,/DEBUG|INFO/ { if ($0 !~ /(DEBUG|INFO)/) { print } }' < logfile
awk 支持此功能非常方便:/startpattern/,/stoppattern/ { }
。不幸的是,如果停止模式与起始模式在同一行匹配,它只会打印出该行,因此需要不同的停止模式。
答案2
尝试这样的操作:
cat importantstuff.log | grep 'File .*, line .*, in .*' -B 1 -A 2
虽然没有准确回答问题,但我认为它完成了任务。
-A
grep 的和标志-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()