Powershell Select-字符串列出多个匹配项
Powershell Select-String list multiple matches
我正在尝试根据某些 XML 文件的单词列表进行一些模式匹配。
我有以下内容:
$Result = Get-ChildItem -Recurse $FullPath\*.xml |
Select-String -Pattern $WordList |
Select-Object Path, Pattern, Line, LineNumber
当同一行代码中有多个匹配项时,我的问题就出现了,
例如,如果 $WordList = "AA"、"AACC",并且该行是:
"This is a test line AA, BB, AACC, KK"
$Result 将只是基于第一个单词 (AA) 的单行匹配。但它不会给我所有三个结果,一个是基于 "AA" 的两个匹配项,另一个是基于 "AACC" 的匹配项,都在同一行。
当前 $Result:
Path Pattern Line LineNumber
** AA This is a test line AA, BB, AACC, KK 12
理想$结果:
Path Pattern Line LineNumber
** AA This is a test line AA, BB, AACC, KK 12
** AA This is a test line AA, BB, AACC, KK 12
** AACC This is a test line AA, AABB, AACC, KK 12
$WordList = "AA","AACC"
$Result = $WordList | Foreach {
Get-ChildItem .\Test\*.txt |
Select-String -Pattern $_ } |
Select-Object Path, Pattern, Line, LineNumber
输出:
$Result
Path Pattern Line LineNumber
---- ------- ---- ----------
** This is a test line AA, BB, AACC, KK 12 1
** This is a test line AA, BB, AACC, KK 12 1
我正在尝试根据某些 XML 文件的单词列表进行一些模式匹配。
我有以下内容:
$Result = Get-ChildItem -Recurse $FullPath\*.xml |
Select-String -Pattern $WordList |
Select-Object Path, Pattern, Line, LineNumber
当同一行代码中有多个匹配项时,我的问题就出现了,
例如,如果 $WordList = "AA"、"AACC",并且该行是:
"This is a test line AA, BB, AACC, KK"
$Result 将只是基于第一个单词 (AA) 的单行匹配。但它不会给我所有三个结果,一个是基于 "AA" 的两个匹配项,另一个是基于 "AACC" 的匹配项,都在同一行。
当前 $Result:
Path Pattern Line LineNumber
** AA This is a test line AA, BB, AACC, KK 12
理想$结果:
Path Pattern Line LineNumber
** AA This is a test line AA, BB, AACC, KK 12
** AA This is a test line AA, BB, AACC, KK 12
** AACC This is a test line AA, AABB, AACC, KK 12
$WordList = "AA","AACC"
$Result = $WordList | Foreach {
Get-ChildItem .\Test\*.txt |
Select-String -Pattern $_ } |
Select-Object Path, Pattern, Line, LineNumber
输出:
$Result
Path Pattern Line LineNumber
---- ------- ---- ----------
** This is a test line AA, BB, AACC, KK 12 1
** This is a test line AA, BB, AACC, KK 12 1