Linux 在 grep 中的每个结果后添加空行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8273636/
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
Add blank line after every result in grep
提问by maverickprac
my grep command looks like this zgrep -B bb -A aa "pattern" *
我的 grep 命令看起来像这样 zgrep -B bb -A aa "pattern" *
I would lke to have output as:
我希望输出为:
file1:line1
file1:line2
file1:line3
file1:pattern
file1:line4
file1:line5
file1:line6
</blank line>
file2:line1
file2:line2
file2:line3
file2:pattern
file2:line4
file2:line5
file2:line6
The problem is that its hard to distinguish when lines corresponding to the first found result end and the lines corresponding to the second found result start.
问题是很难区分第一个找到的结果对应的行何时结束,第二个找到的结果对应的行何时开始。
Note that although man grep says that "--" is added between contiguous group of matches. It works only when multiple matches are found in the same file. but in my search (as above) I am searching multiple files.
请注意,尽管 man grep 说在连续的匹配组之间添加了“--”。仅当在同一文件中找到多个匹配项时才有效。但在我的搜索中(如上),我正在搜索多个文件。
also note that adding a new blank line after every bb+aa+1 line won't work because what if a file has less than bb lines before the pattern.
还要注意,在每个 bb+aa+1 行之后添加一个新的空行是行不通的,因为如果文件在模式之前的行少于 bb 行会怎样。
回答by Michael Krelin - hacker
pipe grep output through
通过管道 grep 输出
awk -F: '{if(f!=)print ""; f=; print [ 11:58 [email protected] ~/test ]$ for f in *; do grep -H "1" $f && echo; done
a:1
b:1
c:1
d:1
[ 11:58 [email protected] ~/test ]$ ll
-rw-r--r-- 1 jon people 2B Nov 25 11:58 a
-rw-r--r-- 1 jon people 2B Nov 25 11:58 b
-rw-r--r-- 1 jon people 2B Nov 25 11:58 c
-rw-r--r-- 1 jon people 2B Nov 25 11:58 d
;}'
回答by chown
There is no option for this in grep
and I don't thinkthere is a way to do it with xargs
or tr
(I tried), but here is a for loop that will do it (for f in *; do grep -H "1" $f && echo; done
):
对此没有选择grep
,我认为没有办法用xargs
or tr
(我试过)来做到这一点,但这里有一个 for 循环可以做到这一点 ( for f in *; do grep -H "1" $f && echo; done
):
sed G
The -H
is to display file names for grep
matches. Change the *
to your own file glob/path expansion string if necessary.
的-H
是显示文件名grep
匹配。*
如有必要,将更改为您自己的文件 glob/path 扩展字符串。
回答by dnfehren
I can't test it with the -A and -B parameters so I can't say for sure but you could try using sed G as mentioned here on Unix StackEx. You'll loose coloring though if that's important.
我无法使用 -A 和 -B 参数对其进行测试,所以我不能确定,但您可以尝试使用 sed G,如Unix StackEx上所述。如果这很重要,你会失去着色。
回答by Vikas Putcha
Try with -c 2; with printing a context I see grep is separating its found o/p
试试 -c 2; 通过打印上下文,我看到 grep 正在分离其找到的 o/p
回答by YenForYang
The problem is that its hard to distinguish when lines corresponding to the first found result end and the lines corresponding to the second found result start.
Note that although man grep says that "--" is added between contiguous group of matches. It works only when multiple matches are found in the same file. but in my search (as above) I am searching multiple files.
问题是很难区分第一个找到的结果对应的行何时结束,第二个找到的结果对应的行何时开始。
请注意,尽管 man grep 说在连续的匹配组之间添加了“--”。仅当在同一文件中找到多个匹配项时才有效。但在我的搜索中(如上),我正在搜索多个文件。
If you don't mind a --
in lieu of a </blank line>
, add the -0
parameter to your grep
/zgrep
command. This should allow for the --
to appear even when searching multiple files. You can still use the -A
and -B
flags as desired.
如果您不介意 a--
代替 a </blank line>
,请将-0
参数添加到您的grep
/zgrep
命令中。--
即使在搜索多个文件时,这也应该允许出现。您仍然可以根据需要使用-A
和-B
标志。
回答by jasonleonhard
Pipe |
any output to:
管道|
任何输出到:
ls | sed G
Example:
例子:
some-stream | grep --group-separator=
If you man sed
you will see
如果man sed
你会看到
G
Append's a newline character followed by the contents of the hold space to the pattern space.
G
将换行符后跟保留空间的内容附加到模式空间。
回答by mork
You can also use the --group-separatorparameter, with an empty value, so it'd just add a new-line.
您还可以使用--group-separator参数,其值为空,因此它只会添加一个换行符。
##代码##