如何在Windows中查找、更新和替换(Powershell、Findstr)
我想要的是: 打开一个文件,用正则表达式查找匹配项,增加匹配项,替换文本中的匹配项,保存文件如何在Windows中查找、更新和替换(Powershell、Findstr),windows,shell,powershell,Windows,Shell,Powershell,我想要的是: 打开一个文件,用正则表达式查找匹配项,增加匹配项,替换文本中的匹配项,保存文件 是否可以使用Powershell或FINDSTR命令执行此操作?这将是Perl中的1行程序,FWIW这将是Perl中的1行程序,FWIW "*original file*" gc matchtest.txt $match_pat = "^(Match\stext\s)(\d+)" $newfile = @() gc matchtest.txt |% { if ($_ -match $mat
是否可以使用Powershell或FINDSTR命令执行此操作?这将是Perl中的1行程序,FWIW这将是Perl中的1行程序,FWIW
"*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