Nat*_*ram 10 powershell prepend powershell-core
我正在生成两个文件,userscript.meta.js并且userscript.user.js. 我需要将 的输出userscript.meta.js放置在 的开头userscript.user.js。
Add-Content似乎不接受前置参数,并且Get-Content | Set-Content会失败,因为userscript.user.js正在使用Get-Content.
如果物理上可能有一个干净的解决方案,我宁愿不创建中间文件。
如何实现这一目标?
San*_*zon 14
Subexpression 运算符$( )可以计算两个Get-Content语句,然后枚举这些语句并通过管道传递到Set-Content:
$(
Get-Content userscript.meta.js -Raw
Get-Content userscript.user.js -Raw
) | Set-Content userscript.user.js
Run Code Online (Sandbox Code Playgroud)
如果当前目录不是这些文件所在的位置,请考虑使用文件的绝对路径。
比上述方法更简单的方法是将路径按所需顺序放置,因为 和-Path参数-LiteralPath都可以采用多个值:
(Get-Content userscript.meta.js, userscript.user.js -Raw) |
Set-Content userscript.user.js
Run Code Online (Sandbox Code Playgroud)
如果您想摆脱多余的前导或尾随空白,您可以包含String.Trim方法:
(Get-Content userscript.meta.js, userscript.user.js -Raw).Trim() |
Set-Content userscript.user.js
Run Code Online (Sandbox Code Playgroud)
请注意,在上面的示例中,分组运算符( )是强制性的,因为我们需要在通过管道传递到Get-Content 之前Set-Content消耗所有输出。有关更多详细信息,请参阅管道分组表达式。
| 归档时间: |
|
| 查看次数: |
3236 次 |
| 最近记录: |