我如何grep
并在每个匹配的行周围显示前后 5 行?
man grep | grep -C 1 context
:)
man grep | grep -C 1 "\-C"
;)
对于 BSD 或 GNU grep
,您可以使用 -B num
设置匹配前的行数,使用 -A num
设置匹配后的行数。
grep -B 3 -A 2 foo README.txt
如果您希望前后行数相同,可以使用 -C num
。
grep -C 3 foo README.txt
这将显示 3 行之前和 3 行之后。
-A
和 -B
将起作用,-C n
(对于 n
行上下文)或仅 -n
(对于 n
行上下文......只要 n 是 1 到 9)都可以工作.
-n
格式,发现它只在 9
之前有效。对于 15
,它返回 5 行
-A
、-B
或 -C
)。通常,选项说明符后跟一个值(-o a.out
用于在 GCC 中指定输出文件),但它也可以用作简单的开关/标志(-g
用于在 GCC 中启用调试信息)。但是选项之间的空格是可选的,因此对于没有值的选项,可以合并它们 (-ABC
),这意味着 -15
被解释为 -1 -5
(两个单独的选项)并且 -5
覆盖 { 10}。
-n
选项目前在 (GNU grep) 3.1 上适用于超过 9 行。
ack 使用与 grep 类似的参数,并接受 -C
。但通常更适合通过代码进行搜索。
grep
,不确定用它来“做广告”ack
(这确实是一个很棒的工具)是个好主意……
grep astring myfile -A 5 -B 5
这将为“astring”grep“myfile”,并在每次匹配之前和之后显示 5 行
我通常使用
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
ripgrep
如果您关心性能,请使用与 grep
具有相似语法的 ripgrep
,例如
rg -C5 "pattern" .
-C, --context NUM - 在每次匹配之前和之后显示 NUM 行。
还有 -A
/--after-context
和 -B
/--before-context
等参数。
该工具构建在 Rust's regex engine 之上,这使得它非常efficient 处理大数据。
使用 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
如果您经常搜索代码,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
在 /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;
您可以在 grep 命令中使用选项 -A
(之后)和 -B
(之前)。
试试grep -nri -A 5 -B 5 .
nri
应该做什么?实际搜索的是什么?它试图匹配什么?请尽可能通过 editing (changing) your answer 回复,而不是在评论中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像今天写的)。
我以紧凑的方式做到这一点:
grep -5 string file
这相当于:
grep -A 5 -B 5 string file
这是 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
注意:将 a
和 b
变量替换为前后的行数。
对于不支持 grep 的 -A
、-B
和 -C
参数的系统特别有用。
Grep 有一个名为 Context Line Control
的选项,您可以在其中使用 --context
,简单地说,
| grep -C 5
或者
| grep -5
应该做的伎俩
$ grep thestring thefile -5
-5
让您在匹配 'thestring' 的上下 5
行等效于 -C 5
或 -A 5 -B 5
。
让我们用一个例子来理解。我们可以使用带有选项的 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