所以我整理了一些很大的平面文件。
我正在尝试使用PowerShell做的是仅选择具有预期分隔符数量的行,并将其输出到文件中。
这有效:
function fixColDelim([string]$importFile)
{
$goodFile = $importFile.Replace(".txt", "_GOODX1.txt")
$sr = New-Object -TypeName System.IO.StreamReader -ArgumentList $importFile
$sw = New-Object -TypeName System.IO.StreamWriter -ArgumentList $goodFile
$sr.ReadLine() | Out-Null
$sr.ReadLine() | Out-Null
While (-not $sr.EndOfStream) {
$line = $sr.ReadLine().ToString()
$gl = ($line.ToCharArray() | Where-Object {$_ -eq '|'} | Measure-Object).Count
Write-Host $gl
if($gl -eq 350)
{
$sw.WriteLine($sr.ReadLine())
}
}
$sw.close()
$sr.close()
}
但是,它是RBAR,因此它不是处理500mb文件的最有效方法。有什么建议么?
扎克
最佳答案
这是替换正则表达式的替代方法。
$gl = ($line -replace '[^|]','').length
如果您可能嵌套了定界符,则可以更进一步。
$gl = ($line -replace '[^|"]','' -replace '"\|"',"").length
关于powershell - 如何更快地过滤带有某些字符的文件行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43285580/