There’s a GNU program called sponge that soaks up input before writing to a file so you can do something like this: cat myFile | grep "myFilter" | sponge myFile
Is there a powershell equivalent, so I can work on a file in place, without having to pipe to a temporary file?
Thanks
In Powershell, judicious use of parentheses will force an operation to completely finish before passing data to the next command in the pipeline. The default for piping
Get-Contentis to pipe line by line to the next command, but with parentheses it must form a complete data set (e.g., load all lines) before continuing:An alternative that may use less memory (I have not benchmarked it) is to only force the results of
Select-Stringto complete before continuing:You could also assign things to a variable as an additional step. Any technique will load the contents into the Powershell session’s memory, so be careful with big files.
Addendum:
Select-StringreturnsMatchInfoobjects. UsingOut-Fileadds pesky extra blank lines due to the way it tries to format the results as a string, butSet-Contentcorrectly converts each object to its own string as it writes, producing better output. Being that you’re coming from *nix and are used to everything returning strings (whereas Powershell returns objects), one way to force string output is to pipe them through aforeachthat converts them: