从输出中删除空行?
问题描述:
当我运行这个命令时,它返回带有很多空行的输出.
When I run this command, it returns the output with lots of blank lines.
Get-Host | Select Name, Version | Format-List
我尝试使用 ExpandProperty
但它仍然在输出的末尾留下一个空行,有没有办法删除最后一个空行?
I tried using ExpandProperty
but it still leaves me with one blank line at the end of the output, is there a way to remove the last blank line?
Get-Host | Select -ExpandProperty Name, Version | Format-List
答
如果你真的需要Format-List
提供的格式,那么你可以使用Out-String
和 Trim() 去除空行:
If you really need the format that Format-List
provides, then you can use Out-String
and Trim() to get rid of empty lines:
Get-Host | Select Name,Version | Format-List | Out-String | ForEach-Object { $_.Trim() }
但是,根据您的需要,使用 CSV 或 JSON 格式进行进一步处理可能更容易(请参阅 ConvertTo-Json
或 ConvertTo-Csv
).
But, depending on your needs, it's probably easier to just use CSV or the JSON format for further processing (see ConvertTo-Json
or ConvertTo-Csv
).