You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows PowerShell中qmake输出经Out-String后每行新增空行的解决求助

解决PowerShell捕获qmake输出时每行多出空行的问题

可行解决方案

方法1:清理多余回车符

直接在捕获输出后替换掉多余的\r字符,这是最直接的办法:

# 子表达式捕获并清理
$qmakeOutput = (qmake -project -o -) -replace '\r', ''

# Out-String处理并清理
$qmakeOutput = qmake -project -o - | Out-String -Stream | ForEach-Object { $_ -replace '\r', '' } | Out-String

方法2:直接读取原始输出

绕开PowerShell的默认输出解析,用.NET方法读取标准输出:

# 启动qmake并把输出重定向到临时文件
$process = Start-Process qmake -ArgumentList "-project -o -" -NoNewWindow -PassThru -RedirectStandardOutput temp.txt
$process.WaitForExit()
# 读取原始内容
$qmakeOutput = Get-Content temp.txt -Raw
# 删除临时文件
Remove-Item temp.txt

方法3:过滤空行

如果只是要去掉多余空行,也可以先拆分输出为行,过滤掉空行再合并:

$qmakeOutput = qmake -project -o - | Out-String -Stream | Where-Object { $_ -ne '' } | Out-String

问题根源

Windows下qmake输出的换行符是CRLF(\r\n),但PowerShell 5.1处理外部程序标准输出时,会错误地把\r(回车符)也当成换行分隔符,导致原本的一行被拆成了内容行加空行。而ipconfig这类系统工具的输出符合PowerShell的解析逻辑,所以不会出问题。

内容的提问来源于stack exchange,提问作者emkey08

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 05:03:22