如何添加到 PowerShell 中的管道?

How to add to a pipeline in PowerShell?

假设我有两个文件,f1.txtf2.txtf1.txt 需要进行一些更正,之后需要将两个文件一起处理。如何将 f1.txt 更正的输出与 f2.txt 数据合并到管道?

这是一个例子:

Get-Content f1.txt |
% {
    $_ #SOME OPERATION
} # How do I merge this output into the next pipeline?
Get-Content f2.txt |
% {
    #COMBINED OPERATIONS on f1.txt output and f2.txt
} > output.txt

我知道我可以将第一个操作保存到一个临时文件中,然后再次从中读取以进行组合操作:

...
} > temp.txt
Get-Content temp.txt, f2.txt |
...

但是有没有不创建缓冲文件的方法呢?

您可以将多个命令包装在单个 SciptBlock 中并调用它:

& {
    Get-Content f1.txt |
    % {
        $_ #SOME OPERATION
    }
    Get-Content f2.txt
} |
% {
    #COMBINED OPERATIONS on f1.txt output and f2.txt
} > output.txt