Powershell - 查找并替换然后保存

发布于 2025-01-11 16:17:00 字数 1166 浏览 0 评论 0原文

我需要读取 10K+ 个文件,逐行搜索文件,查找单词 SUFFIX 后面的字符串。捕获该字符串后,我需要从文件中删除它的所有痕迹,然后重新保存文件。
通过下面的示例 - 我将捕获 -4541。然后我会将所有出现的 -4541 替换为 NULL。 一旦我替换了所有出现的情况,我就会保存更改。

这是我的数据:

ABSDOMN                                  VER     1 D  SUFFIX -4541

         05 ST-CTY-CDE-FMHA-4541
          10 ST-CDE-FMHA-4541                        9(2)
          10 CTY-CDE-FMHA-4541                       9(3)
         05 NME-CTY-4541                             X(20)
         05 LST-UPDTE-DTE-4541                       9(06)
         05 FILLER                                   X

这是一个启动脚本。我可以显示包含单词 SUFFIX 的行,但无法捕获其后面的字符串。在本例中为-4541

$CBLFileList = Get-ChildItem -Path "C:\IDMS" -File -Recurse 
$regex = "\bSUFFIX\b" 
$treat = $false 
ForEach($CBLFile in $CBLFileList) {
    Write-Host "Processing .... $CBLFile" -foregroundcolor green      
    Get-content -Path $CBLFile.FullName |
    ForEach-Object {
            if ($_ -match $regex) {
                Write-Host "Found Match - $_" -foregroundcolor green
                $treat=$true
        }    
    }

I need to read 10K+ files, search the files line by line, for the string of characters after the word SUFFIX. Once I capture that string I need to remove all traces of it from the file then re-save the file.
With the example below - I would capture -4541. Then I would replace all occurrences of -4541 with NULL.
Once I replace all the occurrences I then save the changes.

Here is my Data:

ABSDOMN                                  VER     1 D  SUFFIX -4541

         05 ST-CTY-CDE-FMHA-4541
          10 ST-CDE-FMHA-4541                        9(2)
          10 CTY-CDE-FMHA-4541                       9(3)
         05 NME-CTY-4541                             X(20)
         05 LST-UPDTE-DTE-4541                       9(06)
         05 FILLER                                   X

Here is a starting script. I can Display the line that has the word SUFFIX but I cannot capture the string after it. In this case -4541.

$CBLFileList = Get-ChildItem -Path "C:\IDMS" -File -Recurse 
$regex = "\bSUFFIX\b" 
$treat = $false 
ForEach($CBLFile in $CBLFileList) {
    Write-Host "Processing .... $CBLFile" -foregroundcolor green      
    Get-content -Path $CBLFile.FullName |
    ForEach-Object {
            if ($_ -match $regex) {
                Write-Host "Found Match - $_" -foregroundcolor green
                $treat=$true
        }    
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2025-01-18 16:17:00

请尝试以下操作:

  • 注意:请务必先备份输入文件,因为它们将就地更新。如果需要与 Set-Content 的默认编码不同,请将 -EncodingSet-Content 一起使用来指定所需的编码。
$CBLFileList = Get-ChildItem -LiteralPath "C:\IDMS" -File -Recurse 
$regex = '(?<=SUFFIX) -\d+'
ForEach ($CBLFile in $CBLFileList) {
  $firstLine, $remainingLines = $CBLFile | Get-Content
  if ($firstLine -cmatch $regex) {
    $toRemove = $Matches[0].Trim()
    & { $firstLine -creplace $regex; $remainingLines -creplace $toRemove } |
      Set-Content -LiteralPath $CBLFile.FullName
  }
}

根据您的反馈,最终对您有用的正则表达式是 (?<=SUFFIX).*$ (可以简化为 (?<=SUFFIX ).+ 在这种情况下),即捕获子字符串 SUFFIX 后面的内容,而不是仅捕获空格后跟 - 和一个或多个数字(\d+)。

Try the following:

  • Note: Be sure to make backup copies of the input files first, as they will be updated in place. Use -Encoding with Set-Content to specify the desired encoding, if it should be different from Set-Content's default.
$CBLFileList = Get-ChildItem -LiteralPath "C:\IDMS" -File -Recurse 
$regex = '(?<=SUFFIX) -\d+'
ForEach ($CBLFile in $CBLFileList) {
  $firstLine, $remainingLines = $CBLFile | Get-Content
  if ($firstLine -cmatch $regex) {
    $toRemove = $Matches[0].Trim()
    & { $firstLine -creplace $regex; $remainingLines -creplace $toRemove } |
      Set-Content -LiteralPath $CBLFile.FullName
  }
}

Based on your feedback, the regex that worked for you in the end was (?<=SUFFIX).*$ (which could be simplified to (?<=SUFFIX).+ in this case), i.e. one that captures whatever follows substring SUFFIX, instead of only capturing a space followed by a - and one or more digits (\d+).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文