本文介绍了grep与不包含关键字的上下文关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想grep通过日志,并收集一个特定的异常堆栈跟踪,但我只想看到那些不包含某些关键字 - 在上下文中。
我不知道在关键字后面的哪一行。
简单的例子 - 给出这个shell代码:
grep -A 2 A<< EOF
A
B
C
R
A
Z
Z
X
EOF
输出是:
A
B
C
-
A
z
z
我想输出为:
A
Z
Z
我想排除在上下文后有'B'的匹配
我该怎么做?使用grep不是必需的,但我只能访问coreutils和perl。
解决方案
这个问题非常适合 awk
:
grep -A2 LOG_FILE | awk -v RS =' - \ n''!/ B / {printf%s,$ 0}'
$
- `-v RS =' - \ n'设置记录分隔符。 / 查找不包含
B
的记录。
I want to grep through logs, and gather a certain exception stacktrace but I want to only see those that do not contain certain keywords in --after-context.I do not know in which line in after-context the keyword is.
Simple example - given this shell code:
grep -A 2 A <<EOF
A
B
C
R
A
Z
Z
X
EOF
the output is:
A
B
C
--
A
Z
Z
I'd like the output to be:
A
Z
Z
I want to exclude any match that has 'B' in after-context
How do I do this? Using grep is not a requirement, though I only have access to coreutils and perl.
解决方案
This problem is a good fit for awk
:
grep -A2 A LOG_FILE | awk -v RS='--\n' '!/B/ { printf "%s", $0 }'
- `-v RS='--\n' sets the record separator.
!/B/
finds records that do not containB
.
这篇关于grep与不包含关键字的上下文关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!