Find and Replace in a Large File

前端 未结 5 1829
时光取名叫无心
时光取名叫无心 2021-02-09 15:52

I want to find a piece of text in a large xml file and want to replace with some other text. The size of the file is around ( 50GB). I want to do this in command line. I am look

5条回答
  •  爱一瞬间的悲伤
    2021-02-09 16:02

    This is my take on it, building on some of the other answers here:

    Function ReplaceTextIn-File{
      Param(
        $infile,
        $outfile,
        $find,
        $replace
      )
    
      if( -Not $outfile)
      {
        $outfile = $infile
      }
    
      $temp_out_file = "$outfile.temp"
    
      Get-Content $infile | Foreach-Object {$_.Replace($find, $replace)} | Set-Content $temp_out_file
    
      if( Test-Path $outfile)
      {
        Remove-Item $outfile
      }
    
      Move-Item $temp_out_file $outfile
    }
    

    And called like so:

    ReplaceTextIn-File -infile "c:\input.txt" -find 'http://example.com' -replace 'http://another.example.com' 
    

提交回复
热议问题