Linux grep 命令在每次匹配后添加结束行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20567667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep command to add end line after every match
提问by Sun
do you have any idea how to add some end line like
你知道如何添加一些结束线吗
"=========================================================================================="
after every match
每场比赛后
tail -f error.log -n 2000 | grep -B 10 -A 25 'Exception:'
this command prints all Exceptions log but i likes to see one seperator line for each exception log.
此命令打印所有异常日志,但我喜欢为每个异常日志看到一行分隔符。
回答by Chris Seymour
You want the following option:
您需要以下选项:
--group-separator=SEP
Use SEP as a group separator. By default SEP is double hyphen (--).
--group-separator=SEP
使用 SEP 作为组分隔符。默认情况下,SEP 是双连字符 (--)。
Demo:
演示:
$ cat file
before
exception 1
after
foo
bar
before
exception 2
after
$ grep -A 1 -B 1 --group-separator======================== exception file
before
exception 1
after
=======================
before
exception 2
after
回答by Floris
For people like myself who have a 'very old' grep that doesn't include the --group-separator
option, the following seems to be an acceptable workaround.
对于像我这样拥有不包含该--group-separator
选项的“非常古老”的 grep 的人来说,以下似乎是可以接受的解决方法。
Noticing that grep
(my version, 2.5.1) does produce a "small, default" separator between groups (--
), you can easily replace that string with a string of your choice:
注意到grep
(我的版本,2.5.1)确实在组 ( --
)之间产生了一个“小,默认”分隔符,您可以轻松地用您选择的字符串替换该字符串:
tail -f rms.log -n 2000 | grep -B 10 -A 25 'Exception:' | sed 's/^--$/=================/'
This does replace the --
with ============
这确实更换--
用============
Obviously you can modify this to your liking. If you have the option of using --group-separator
(@sudo_O's answer) that is obviously preferable.
显然,您可以根据自己的喜好修改它。如果您可以选择使用--group-separator
(@sudo_O 的回答),那显然更可取。
EDITreading the comments below the question, I realize that when @starrify updated his comment (which I had not noticed before) his comment essentially pointed directly to this solution - so I feel I own him a tip of the hat...
编辑阅读问题下方的评论,我意识到当@starrify 更新他的评论(我之前没有注意到)时,他的评论基本上直接指向了这个解决方案 - 所以我觉得我拥有他的一顶帽子......
回答by slayedbylucifer
I prefer sed
for text manipulation:
我更喜欢sed
文本操作:
# cat test
1
Exception
2
Exception
3
4
Exception
5
Exception
6
7
8
Exception
9
# sed -i '/Exception/a =========================' test
# cat test
1
Exception
=========================
2
Exception
=========================
3
4
Exception
=========================
5
Exception
=========================
6
7
8
Exception
=========================
9
回答by Ed Morton
You posted a command that doesn't do what you want and described it's output, but you didn't tell us what you DO want so this is a guess but maybe it'll be useful. It prints the 2 lines before and 3 lines after some regexp:
您发布了一个不能执行您想要的命令并描述了它的输出的命令,但是您没有告诉我们您想要什么,所以这是一个猜测,但也许它会有用。它打印一些正则表达式之前的 2 行和之后的 3 行:
$ cat file
a
b
c
d
FOO
e
f
g
h
i
j
FOO
k
l
m
n
o
$ awk -v re="FOO" -v b=2 -v a=3 'NR==FNR{line[FNR]=##代码##;next} ##代码## ~ re{for (i=(FNR-b);i<=(FNR+a);i++) print line[i]; print "=====" }' file file
c
d
FOO
e
f
g
=====
i
j
FOO
k
l
m
=====