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

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -