PowerShell is slow (much slower than Python) in large Search/Replace operation?

前端 未结 5 1213
长情又很酷
长情又很酷 2021-02-02 12:41

I have 265 CSV files with over 4 million total records (lines), and need to do a search and replace in all the CSV files. I have a snippet of my PowerShell code below that does

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 13:17

    You may want to try the following command:

    gci C:\temp\csv\*.csv | % { (gc $_) -replace $SearchStr, $ReplaceStr | out-file $_}
    

    In addition, some strings may require escape characters, hence you should use [regex]Escape to generate strings with escape characters built in. The code would look like:

    gci C:\temp\csv\*.csv | % { (gc $_) -replace $([regex]::Escape($SearchStr)) $([regex]::Escape($ReplaceStr)) | out-file $_}
    

提交回复
热议问题