Processing huge volume data file using powershell

104 Views Asked by At

I am trying to perform a replace operation on a data file which is 4GB. But I am not even able to read this file due to memory exception. The following command gives a memory error.

$edwfile = (Get-Content C:\Users\tomgeorg\Desktop\edw_ord_extr_3x_SIQP_20181021.182305\edw_ord_extr_3x_SIQP_20181021.182305.dat -Raw ) 

Is there any alternative commands or tricks to process huge file.

I want to run the following replace pattern on each line in the file.basically I want to remove all the unwanted special characters.

-replace  "[$([char]0x00)-$([char]0x09)$([char]0x0B)-$([char]0x1F)$([char]0x7F)-$([char]0xFF)]","?"

system details

enter image description here

2

There are 2 best solutions below

0
Mike Twc On BEST ANSWER

Below is the sample solution with streams. It reads file line by line and then add updated line to a new file.

$reader = [System.IO.StreamReader]"C:\temp\OriginalFile.txt"
$writer = [System.IO.StreamWriter]"C:\temp\UpdatedFile.txt"

while (!$reader.EndOfStream) {

$writer.WriteLine(($reader.ReadLine() -replace '\|', ";"))

}

$reader.Close()
$writer.Close()
0
Maximilian Burszley On

Assuming you are expecting to work on one line at a time, you'll want to use the pipeline for your task:

$path = '~\Desktop\edw_ord_extr_3x_SIQP_20181021.182305\edw_ord_extr_3x_SIQP_20181021.182305.dat'
Get-Content -Path $path | ForEach-Object {
    # do something line-by-line with the file
} | # -> do something else with the output

Without knowing what you're doing with the file, it's hard to give a more complete answer.