【发布时间】:2021-04-03 07:49:58
【问题描述】:
我发现我可以使用 grep -A10 "search string" file.txt 在文件中找到搜索字符串之后显示 10 行,或者使用 grep -B10 "search string" file.txt 显示之前的 10 行。如何设置它以显示比赛之前或之后的所有行?
例如,我有这个文件:
This is some text.
This is some more text.
This is some text.
This is some more text.
This is some text.
This is some yet more text.
This is some text.
---
This is some more text.
This is some text.
This is some more text.
在第一次搜索中,我需要打印在“---”之前找到的所有内容,无论有多少行。
而且我需要在第二次搜索中打印在“---”之后找到的所有内容,无论之后有多少行。
【问题讨论】:
-
grep 做不到。
-
这可能对 GNU grep 有所帮助:
grep -zo -- '.*---' file和grep -zo -- '---.*' file