我有一组文本文件,其结构如下。
问题陈述:每行可以是80个字符或少于80个字符。我想删除任何8位数字,如果它在行尾找到,则其长度为80个字符。< / p>
例如下面。第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
答案 0 :(得分:2)
有很多方法可以解决这个问题。一个解决方案是:
#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:\" }
如果您还想修剪所有行以在开头和结尾删除额外的空格,您只需将$_ -replace ...
行更改为:
($_ -replace "^(.{72})([0-9]{8})$", '$1').Trim()
说实话,我不明白为什么你需要匹配80个字符,如果那是8digit ID的唯一场景。您可以简单地替换字符串末尾的所有8位数ID。要试用它,请将以上示例中的$_ -replace ...
行替换为:
$_ -replace '[0-9]{8}$'
答案 1 :(得分:1)
我做了以下,似乎有效:
#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