hazrmard hazrmard - 3 months ago 9
PowerShell Question

How to add to a pipeline in powershell?

Let's say I have 2 files

f1.txt
and
f2.txt
.
f1.txt
has some corrections that need to be made, after which both files will need to be processed together. How to I merge the output of
f1.txt
corrections to the pipeline with
f2.txt
data? Here is an illustration:

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


Edit: I understand I can save the first operation into a temporary file and read from it again for the combined operation:

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


But is there a way to do it without creating buffer files?

Answer

You can wrap multiple commands in single SciptBlock and invoke it:

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