powershell - How can I filter lines of a file with a certain number of characters faster? -
so have jacked flat files rather large.
what i'm trying do, using powershell, select lines have expected amount of delimiters , output them file.
this works:
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() } however, it's rbar it's not efficient method against 500mb file. suggestions?
zach
here alternative replace regex.
$gl = ($line -replace '[^|]','').length if might have nested delimiters, take further.
$gl = ($line -replace '[^|"]','' -replace '"\|"',"").length
Comments
Post a Comment