【发布时间】:2017-02-25 22:08:37
【问题描述】:
我正在使用以下命令:
get-content C:\somepath\Setup.csproj |
select-string -pattern '<None Include="packages.config" />' -notmatch |
Out-File -filepath C:\somepath\Setup.csproj -force
这个想法是删除 Setup.csproj 中包含文本 <None Include="packages.config" /> 的所有行。
但是,当我运行上述命令时,我的 Setup.csproj 文件被清空(它从有一堆文本变为没有测试)。
但如果我将命令更改为输出到新文件 Setup2.csproj,则新文件已创建并具有我想要的内容:
get-content C:\somepath\Setup.csproj |
select-string -pattern '<None Include="packages.config" />' -notmatch |
Out-File -filepath C:\somepath\Setup2.csproj -force
这很好用,我想我可以删除原来的并将 Setup2.csproj 重命名为 Setup.csproj,但如果可以一步完成,我会更喜欢它。
有没有办法使用上面的 get-content 和 select-string 方法,然后在 SAME 文件上调用 Out-File?
(注意,我从question得到了上面的例子。)
【问题讨论】:
-
(get-content C:\somepath\Setup.csproj) -
@PetSerAl 和 wOxxOM - 就是这样!也学到了一些新东西。如果您作为答案发布,我会接受!
标签: powershell