使用 PowerShell Select-String 在文本文件的同一行中查找多个字符串
Finding multiple strings on the same line in text files with PowerShell Select-String
我试图在我们的源代码控制文件(1000 个)中找到所有 SQL 服务器对象名称(大约 800 个)。当我尝试使用 Select-String 时,它会找到一个字符串但随后它停止处理找到匹配项的行上的文件。这是我得到的一小部分样本。它也应该 return 匹配 'was'。
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
您可以使用正则表达式而不是字符串数组来完成此操作:
('This is a test. How was your test?' | Select-String -Pattern '\btest\b|\bwas\b' -AllMatches).Matches
我试图在我们的源代码控制文件(1000 个)中找到所有 SQL 服务器对象名称(大约 800 个)。当我尝试使用 Select-String 时,它会找到一个字符串但随后它停止处理找到匹配项的行上的文件。这是我得到的一小部分样本。它也应该 return 匹配 'was'。
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
您可以使用正则表达式而不是字符串数组来完成此操作:
('This is a test. How was your test?' | Select-String -Pattern '\btest\b|\bwas\b' -AllMatches).Matches