如何在 PowerShell 中“grep”并显示周围的行/上下文?

Dan*_*Dan 2 powershell command full-text-search cmd command-line-arguments

什么是 Powershell cmd: findstr 的“上下文行控制”设置,或者什么是替代 Powershell cmd,它可以让我在某些输出中找到一个字符串,还可以方便地打印周围的行?

在 GNU/Linux 上,我会这样做: grep -A 5 -B 5 somestring file.txt

下面的命令搜索字符串“four”,但要求 grep 在找到的行上方显示 1 行,在找到的包含字符串的行下方显示 2 行。

$ grep -A 2 -B 1 four tmp.text
three
four
five
six
$ cat tmp.text
one
two
three
four
five
six
seven
Run Code Online (Sandbox Code Playgroud)

谢谢

Dan*_*Dan 5

我在这里找到了答案:https : //communary.net/2014/11/10/grep-the-powershell-way/

您可以搜索示例文件,如:

PS C:\Users\dan> Select-String four .\tmp.txt -Context 1,3

  tmp.txt:3:three
> tmp.txt:4:four
  tmp.txt:5:five
  tmp.txt:6:six
  tmp.txt:7:seven
Run Code Online (Sandbox Code Playgroud)