0
我想要什麼: 打開文件,找到與正則表達式的匹配項,增加匹配項,替換文本中的匹配項,保存文件。如何在Windows中查找,更新和替換(Powershell,Findstr)
是否可以使用Powershell或FINDSTR命令執行此操作?
我想要什麼: 打開文件,找到與正則表達式的匹配項,增加匹配項,替換文本中的匹配項,保存文件。如何在Windows中查找,更新和替換(Powershell,Findstr)
是否可以使用Powershell或FINDSTR命令執行此操作?
"*original file*"
gc matchtest.txt
$match_pat = "^(Match\stext\s)(\d+)"
$newfile = @()
gc matchtest.txt |% {
if ($_ -match $match_pat) {
$incr = 1 + $matches[2]
$newfile += $_ -replace $match_pat,($matches[1] + $incr)
}
else {$newfile += $_}
}
$newfile | out-file matchtest.txt -force
"`n*new file*"
gc matchtest.txt
*original file*
Not match 121
Match text 127
Not match 123
*new file*
Not match 121
Match text 128
Not match 123
這將是Perl,FWIW中的1班輪 –