powershell修改文件中某個字符串(-replace操作符)
阿新 • • 發佈:2018-02-10
字符 是個 腳本執行 pos -s get replace lac 結果
使用-replace操作符
test.txt文件內容
1234
hello world
aaa "hhh"
fwdbz
test.ps1腳本文件
$(Get-Content E:\demo\test.txt) |
Foreach-Object {$_ -replace ("1234","5678")} |
Foreach-Object {$_ -replace ("hello world","hello java")} |
Out-File E:\demo\test.txt
這是個簡單的字符串替換,將test.txt文件中的1234替換成5678,"hello world"替換成"hello java"
上面是無參數腳本執行,修改文件中字符串
下面這個是傳遞參數修改文件中字符串
test1.ps1腳本文件
#執行操作 test1.ps1 1234 5678參數之間通過空格隔開,操作結果如下將test.txt文件中的1234 替換為 5678
#輸出參數1,2
Write-Host "$($args[0])" "$($args[2])"
$p1 = "$($args[0])"
$p2 = "$($args[1])"
$(Get-Content E:\demo\test.txt) |
Foreach-Object {$_ -replace ($p1,$p2)} |
Out-File E:\demo\test.txt
powershell修改文件中某個字符串(-replace操作符)