Regex Powershell根据长度替换行上的文本

Regex Powershell根据长度替换行上的文本,regex,powershell,match,Regex,Powershell,Match,我有一组文本文件,其结构如下 问题陈述:每行可以是80个字符或少于80个字符。我想删除任何8位数字,如果它是在行尾找到的,因为它的长度是80个字符 例如下面的例子。第1行和第2行各有80个字符,末尾有8位数字。因此,只需删除最后8位数字,即00100001和00100002。对于第3行和第4行,什么也不做。对于第5行,再次删除最后8个数字,即00100024。对于第6行,什么也不做。带括号的材料(长度为80行1)仅用于说明,不属于任何一行 ABCD some text 0010000

我有一组文本文件,其结构如下

问题陈述:每行可以是80个字符或少于80个字符。我想删除任何8位数字,如果它是在行尾找到的,因为它的长度是80个字符

例如下面的例子。第1行和第2行各有80个字符,末尾有8位数字。因此,只需删除最后8位数字,即00100001和00100002。对于第3行和第4行,什么也不做。对于第5行,再次删除最后8个数字,即00100024。对于第6行,什么也不做。带括号的材料(长度为80行1)仅用于说明,不属于任何一行

ABCD   some text     00100001  (length 80 Line 1)
EFGH                 00100002  (Length 80 Line 2)
ABCD   Some text               (Length less than 80 Line 3)
XYZD                           (Length less than 80 Line 4)
MNOP                 00100024  (Length 80 Line 5)
ABCD                           (Length less than 80 Line 6)
以上结果

ABCD   some text     
EFGH                 
ABCD   Some text     
XYZD                 
MNOP                 
ABCD                 

到目前为止,我只能将其设置为读取循环中的所有文件,但无法实际更改文件的内容。我肯定我有问题,没有文件的事情

**
#ERROR REPORTING ALL
Set-StrictMode -Version latest
$path     = "d:\users\desktop\D2d_Try"
$files    = Get-Childitem $path -Recurse | Where-Object { !($_.psiscontainer) }

    Function getStringMatch
    {
      # Loop through all *.txt files in the $path directory
      Foreach ($file In $files)
      {
       $content = Get-Content $file.fullName 

    $content | foreach-object { if($_.length -eq 80) { if($_ -match "^.{72}([0-9]{8})") 
    { 
    $_ -replace "$matches[1]"," " | out-file "c:\$file" -append
    }
    }
    }

    }
    }

    getStringMatch

有很多方法可以做到这一点。一种解决办法是:

#ERROR REPORTING ALL
Set-StrictMode -Version latest
$path = "d:\users\desktop\D2d_Try"

#Creating function first.
#A function should not depend on a variable outside the function ($files in this case)
Function getStringMatch([System.IO.FileInfo]$File, $OutputPath)
{
    Get-Content $File.fullName | ForEach-Object { 
        #The following replace regex will remove the numbers if they are there and the length is 80, if not it will return it as it was.
        $_ -replace "^(.{72})([0-9]{8})$", '$1'
    } | Set-Content -Path (Join-Path $OutputPath $File.Name)
}


$files = Get-Childitem $path -Recurse | Where-Object { !($_.psiscontainer) } | % { getStringMatch -File $_ -OutputPath "C:\" }
如果您还想修剪所有行以消除开头和结尾处的多余空白,只需将
$\uu-replace…
行更改为:

($_ -replace "^(.{72})([0-9]{8})$", '$1').Trim()
老实说,我不明白为什么你需要匹配80个字符,如果这是唯一一个8位ID存在的情况。您可以简单地替换字符串末尾的所有8位ID。要尝试,请将上面示例中的
$\uu-replace…
行替换为:

$_ -replace '[0-9]{8}$'

我做到了以下几点,似乎奏效了:

#ERROR REPORTING ALL
Set-StrictMode -Version latest
$path     = "d:\users\desktop\Cobol_D2d"
$files    = Get-Childitem $path -Recurse | Where-Object { !($_.psiscontainer) }

Function getStringMatch
{
  # Loop through all *.txt files in the $path directory
  Foreach ($file In $files)
  {
   (Get-Content $file.fullName) -replace '[0-9]{8}$',' ' | set-content $file.fullname 

}
}

getStringMatch

因为看起来你使用了弗罗德·F.的答案,所以你把它标记为答案是恰当的。