有效的代码,因为结果。
$output_file = ‘results.txt’
$listtomatch = @("WORD_2", "PHRASE2","WORD", "PHRASE")
select-string -Path filetosearchin.txt -Pattern $listtomatch -CaseSensitive | % { $_.Matches } | % { $_.Value }
代码不起作用,因为它仅返回单词和短语的所有实例,但不会返回word_2 norther2:
$output_file = ‘results.txt’
$listtomatch = @("WORD", "PHRASE","WORD_2", "PHRASE2")
select-string -Path filetosearchin.txt -Pattern $listtomatch -CaseSensitive | % { $_.Matches } | % { $_.Value }
字符串的序列会干扰这些串联的方式,从而产生不同的结果?
我曾期望上面两个代码给出相同的结果:所有字符串的实例:word_2,phroase2,word和pherse
让PowerShell创建交替(|(,还包括-AllMatches
参数:
## Q:Test2019 731SO_57297393.ps1
$listtomatch = @("WORD_2","PHRASE2","WORD","PHRASE")
Select-String -Path filetosearchin.txt -Pattern ($listtomatch -join '|') -CaseSensitive -AllMatches|
% { $_.Matches } | % { $_.Value }
使用输入文件:
> Get-Content .filetosearchin.txt
1:"WORD_2","PHRASE2","WORD","PHRASE"
2:"PHRASE2","WORD","PHRASE","WORD_2"
3:"WORD","PHRASE","WORD_2","PHRASE2"
4:"PHRASE","WORD_2","PHRASE2","WORD"
脚本产生:
> .SO_57297393.ps1
WORD_2
PHRASE2
WORD
PHRASE
PHRASE2
WORD
PHRASE
WORD_2
WORD
PHRASE
WORD_2
PHRASE2
PHRASE
WORD_2
PHRASE2
WORD