如何在 PowerShell 中 "grep" 并显示周围的 lines/context?

How to "grep" in PowerShell and also show surrounding lines/context?

Powershell cmd 的 "Context Line Control" 设置是什么: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

谢谢

我在这里找到了答案: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