使用PowerShell Select-String在文本文件中查找同一行上的多个字符串

时间:2016-10-28 14:16:29

标签: powershell select-string

我试图在我们的源代码控制文件中找到所有SQL Server对象名称(大约800个)(1000')当我尝试使用Select-String时,它会找到一个字符串然后停止在发现匹配的行上处理文件。以下是我收到的一小部分样本。它应该返回一个匹配'是'同样。

Use the Select-String cmdlet to process both of the words 'test' and 'was' in the same sentence and all it returns is the matches for 'test'.

('This is a test. How was your test?' | Select-String -Pattern @('test','was') -AllMatches).Matches

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 10
Length   : 4
Value    : test

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 29
Length   : 4
Value    : test

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式而不是字符串数组来完成此操作:

('This is a test. How was your test?' | Select-String -Pattern '\btest\b|\bwas\b' -AllMatches).Matches