ChatGPT解决这个技术问题 Extra ChatGPT

grep 一个文件,但显示几个周围的行?

我如何grep 并在每个匹配的行周围显示前后 5 行?

为此,我保留了一份 Brendan Gregg's perl script。效果很好。
有关适用于 Solaris 的解决方案,请查看 this link
man grep | grep -C 1 context :)
man grep | grep -C 1 "\-C" ;)
@StvnW ...我不知道是否调用它 meta (在更一般的情况下,而不是 SO 上下文中),或者叫什么。您通过展示如何使用答案来找到答案来回答问题。

V
Vladyslav

对于 BSDGNU grep,您可以使用 -B num 设置匹配前的行数,使用 -A num 设置匹配后的行数。

grep -B 3 -A 2 foo README.txt

如果您希望前后行数相同,可以使用 -C num

grep -C 3 foo README.txt

这将显示 3 行之前和 3 行之后。


这很好,但不幸的是 Solaris grep 不支持。请参阅 solaris 的链接:unix.com/solaris/33533-grep-display-few-lines-before-after.html
好的,但是如果想在比赛结束后显示所有输出行怎么办? grep -A0 和 grep -A-1 不要削减它......
尽管在我的手册页中提到,但由于某种原因对我不起作用。
如果您是 HP-UX 环境,则任何 grep 版本都不会像在 Solaris 中那样工作。能够使用 Solaris 链接,但在该链接中将 nawk 替换为 awk。
-n 用于行号,但对于某些版本的 grep -n# 将显示 # 行号周围的行(如 -c)。这是一个有用的快捷方式,当我需要上下文时,它是我的首选。
T
Trevor Boyd Smith

-A-B 将起作用,-C n(对于 n 行上下文)或仅 -n(对于 n 行上下文......只要 n 是 1 到 9)都可以工作.


我尝试了 -n 格式,发现它只在 9 之前有效。对于 15,它返回 5 行
@DeepakMahakale 这可能与 POSIX 程序通常如何解析命令行参数/选项有关。选项说明符是单个字符(例如 -A-B-C)。通常,选项说明符后跟一个值(-o a.out 用于在 GCC 中指定输出文件),但它也可以用作简单的开关/标志(-g 用于在 GCC 中启用调试信息)。但是选项之间的空格是可选的,因此对于没有值的选项,可以合并它们 (-ABC),这意味着 -15 被解释为 -1 -5(两个单独的选项)并且 -5 覆盖 { 10}。
-5 比 -A 5 -B 5 都快。它们不能一起使用。如果您选择 -A 或 -B 或 -C 而不是 -9,它对脚本的其他读者来说更清晰。
-n 选项目前在 (GNU grep) 3.1 上适用于超过 9 行。
A
Andy Lester

ack 使用与 grep 类似的参数,并接受 -C。但通常更适合通过代码进行搜索。


ack 也支持 -A -B。
grep 的优点是默认安装在许多系统上。
@MarcZ:没错, grep 比 ack 更可能默认安装,但 ack 是一个可移植的独立脚本。无需编译或安装在 /usr/bin/ 等系统目录中。一旦下载并放入 $PATH 中列出的目录(及其执行权限位集),它应该立即工作。 (不需要 sudo 或 root 权限即可让 ack 工作。)
这个问题是关于 grep,不确定用它来“做广告”ack(这确实是一个很棒的工具)是个好主意……
为什么通过代码搜索更好? Can you elaborate 你的答案有点? (但没有“编辑:”、“更新:”或类似的 - 答案应该看起来好像是今天写的。)
d
dbr
grep astring myfile -A 5 -B 5

这将为“astring”grep“myfile”,并在每次匹配之前和之后显示 5 行


“grep astring myfile -C 5” 会做同样的事情
哇,太棒了! A = 之后,B = 之前,C = 上下文
C
Csa77

我通常使用

grep searchstring file -C n # n for number of lines of context up and down

许多像 grep 这样的工具也有非常棒的 man 文件。我发现自己经常提到 grep's man page,因为您可以用它做很多事情。

man grep

许多 GNU 工具还有一个 info page,除了手册页之外,它可能还有更多有用的信息。

info grep

k
kenorb

ripgrep

如果您关心性能,请使用与 grep 具有相似语法的 ripgrep,例如

rg -C5 "pattern" .

-C, --context NUM - 在每次匹配之前和之后显示 NUM 行。

还有 -A/--after-context-B/--before-context 等参数。

该工具构建在 Rust's regex engine 之上,这使得它非常efficient 处理大数据。


c
chtenb

使用 grep

$ grep --help | grep -i context
Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM

您没有阅读接受的答案吗?你只是在重复一个已经有近 10 年历史的问题已经说过的话......
哦,对不起洋井。但我没有阅读任何关于 grep 的帮助部分以检索答案的信息。
@Yokai除了Chiel 10 Brinke所说的之外,接受的答案没有提到长选项
s
sb813322

如果您经常搜索代码,AG the silver searcher 比 grep 更有效(即更快)。

您可以使用 -C 选项显示上下文行。

例如:

ag -C 3 "foo" myFile

line 1
line 2
line 3
line that has "foo"
line 5
line 6
line 7

您在哪个系统(包括发行版名称和版本)上尝试过?它是默认安装的吗?如果没有,有哪些安装说明? Ubuntu MATE 20.04 (Focal Fossa) 默认不安装它。
i
i_want_more_edits

/some/file.txt 中搜索“17655”,显示前后 10 行上下文(使用 Awk),输出前面是行号,后跟一个冒号。当 grep 不支持 -[ACB] 选项时,在 Solaris 上使用它。

awk '

/17655/ {
        for (i = (b + 1) % 10; i != b; i = (i + 1) % 10) {
                print before[i]
        }
        print (NR ":" ($0))
        a = 10
}

a-- > 0 {
        print (NR ":" ($0))
}

{
        before[b] = (NR ":" ($0))
        b = (b + 1) % 10
}' /some/file.txt;

i
i_want_more_edits

您可以在 grep 命令中使用选项 -A(之后)和 -B(之前)。

试试grep -nri -A 5 -B 5 .


这与以前的答案有什么不同?选项 nri 应该做什么?实际搜索的是什么?它试图匹配什么?请尽可能通过 editing (changing) your answer 回复,而不是在评论中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像今天写的)。
P
Peter Mortensen

我以紧凑的方式做到这一点:

grep -5 string file

这相当于:

grep -A 5 -B 5 string file

k
kenorb

这是 awk 中的 @Ygor solution

awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=3 a=3 s="pattern" myfile

注意:将 ab 变量替换为前后的行数。

对于不支持 grep 的 -A-B-C 参数的系统特别有用。


非常好。是的,它看起来更复杂,但它实际上是跨平台的。在 AIX 和 HP-UX 上测试。
N
Noctis

Grep 有一个名为 Context Line Control 的选项,您可以在其中使用 --context,简单地说,

| grep -C 5

或者

| grep -5

应该做的伎俩


k
kenorb
$ grep thestring thefile -5

-5 让您在匹配 'thestring' 的上下 5 行等效于 -C 5-A 5 -B 5


t
tushar

让我们用一个例子来理解。我们可以使用带有选项的 grep:

-A 5  # this will give you 5 lines after searched string.
-B 5  # this will give you 5 lines before searched string.
-C 5  # this will give you 5 lines before & after searched string

例子。 File.txt 包含 6 行,以下是操作。

[abc@xyz]~/% cat file.txt # print all file data
this is first line
this is 2nd line
this is 3rd line
this is 4th line
this is 5th line
this is 6th line

[abc@xyz]~% grep "3rd" file.txt # we are searching for keyword '3rd' in the file
this is 3rd line

[abc@xyz]~% grep -A 2 "3rd" file.txt # print 2 lines after finding the searched string
this is 3rd line
this is 4th line
this is 5th line

[abc@xyz]~% grep -B 2 "3rd" file.txt # Print 2 lines before the search string.
this is first line
this is 2nd line
this is 3rd line

[abc@xyz]~% grep -C 2 "3rd" file.txt # print 2 line before and 2 line after the searched string 
this is first line
this is 2nd line
this is 3rd line
this is 4th line
this is 5th line