【问题标题】:Find and Replace Test in Multiple Files在多个文件中查找和替换文本
【发布时间】:2013-01-29 18:20:45
【问题描述】:

我考虑为一个文件执行此操作,我非常喜欢 powershell 提供的解决方案:

Get-Content test.txt | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt

有没有一种方法可以获取文件列表的内容,执行相同的搜索和替换,并生成第二组文件?

【问题讨论】:

    标签: regex powershell batch-file


    【解决方案1】:

    这完成了我想要的:

    (Get-Content *.txt) | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content *.txt
    

    【讨论】:

      【解决方案2】:

      试试这个 --

      dir *.txt | % {[IO.File]::ReadAllText($_).Replace('bar', 'test') | sc $_} 
      

      【讨论】:

        猜你喜欢
        • 2013-12-26
        • 2014-01-14
        • 2011-07-29
        • 2012-08-29
        • 2014-08-22
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多