我发现此讨论正在处理命令行输出,而不是变量。我的文本在变量中。
我试图在其中找到带有单词的行Melody
。
$output = 'This is some data
Only the lines with Melody should be included
For instance Mozart is a Melody
But Justin Biber Beauty and the Beast is not
Neither is anything Bieber ever did'
$skimmed = ''
$output | Where-Object { $_ -match '.*Melody.*' } | ForEach-Object {
$skimmed = $skimmed + $_ }
Write-Host $skimmed
它正在写的输出如下
This is some data
Only the lines with Melody should be included
For instance Mozart is a Melody
But Justin Biber Beauty and the Beast is not
Neither is anything Bieber ever did
如何只挑选与我的图案匹配的线?
首先将文本块$output
分成几行:
$output = 'This is some data
Only the lines with Melody should be included
For instance Mozart is a Melody
But Justin Biber Beauty and the Beast is not
Neither is anything Bieber ever did' -split '\r?\n'
\r?\n
将匹配所有换行符,无论它们是CRLF,LF还是CR
PS我将剩余的代码更改为这样的事情:
$skimmed = $output | Where-Object { $_ -match 'Melody' } # or use $_ -like '*Melody*'
Write-Host (-join $skimmed)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句