显示匹配行的前后 N 行? [英] Show the previous and after N lines each for the matched lines?
本文介绍了显示匹配行的前后 N 行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下命令将显示与模式匹配的行.
The following command will show the lines match the pattern.
cat file.txt | select-string -pattern "..."
如何显示匹配行的前三行和后三行?(就像grep的-C3
参数.)也可以打印出行号吗?
How to show the previous and after 3 lines each for the matched lines? (Like the -C3
parameter of grep.) Also is it possible to print out the line number?
顺便说一句,这个命令比 grep 慢很多?
BTW, the command is much slower comparing with grep?
推荐答案
在Select-String
的-Path
参数中指定文件名,并使用-Context
开关指定前后包含的行数.
Specify the filename in the -Path
parameter of Select-String
, and use the -Context
switch to specify the number of lines before and after to include.
select-string -path file.txt -pattern "..." -Context 1,3
文件名和行号将包含在输出中
The file name and line number will be included in the output
这篇关于显示匹配行的前后 N 行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文