简体   繁体   English

如何在Windows(Powershell,Findstr)中进行查找,更新,替换

[英]How to make a find,update,replace in Windows (Powershell,Findstr)

What I want : open a file, find a match with a regex,increment the match,replace the match in text,save the file. 我想要的是:打开一个文件,使用正则表达式查找匹配项,增加匹配项,将匹配项替换为文本,然后保存文件。

Is it possible to do this with Powershell or the FINDSTR command ? 是否可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM