[英]Find replace regex string using powershell (in cmd)
我想在 txt 文件中找到正則表達式字符串,將其替換為其他正則表達式字符串並將其輸出到新的 txt 文件。 我想在 cmd(批處理文件)中使用 powershell 命令。
現在我這樣做了,它有效,但不能添加新行:
set Test1=.\Test1.txt
powershell -command "& {Get-ChildItem .\LogFULL.txt | Get-Content | Foreach-Object {$_ -replace '\[\+\]PS PLUS:', 'PS PLUS: ' -replace '\[\+\] Country', 'Region' -replace '================TRANSACTIONS================','----- Games -----' -replace '================TRANSACTIONS END============','-----\r\nPlay Time\: 20 Minutes--------------------'}} | Set-Content -encoding Default .\Test1.txt"
我嘗試了不同的方法,比如`r`n
或添加和刪除 -raw、-r、foreach 等,但無法處理。
轉義字符必須在使用 QUOTATION MARK 字符的字符串中。 此外,不需要調用運算符 (&)。
powershell -NoLogo -NoProfile -Command ^
"Get-ChildItem .\Test1.txt |" ^
"Get-Content |" ^
"Foreach-Object { $_ -replace 'is', \"was`r`nwas\" } |" ^
"Set-Content -encoding Default .\Test2.txt"
結果是:
PS C:\src\t> Get-Content -Path .\Test1.txt
now is the time
PS C:\src\t> Get-Content -Path .\Test2.txt
now was
was the time
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.