0

I have a large csv file with 41000 rows of data. Within this I have to go through and inspect each row for a particular string PRCXX and if the row contains that in column 6 then I take the value of column 10 and replace the value in column 9 with it.

I have code that works however it takes a very long time to parse through line by line. I am looking for some help to try and optimize it. I have tried switching to a ForEach loop however I am not sure how exactly to get it to work with what I am trying to accomplish and haven't been able to find any examples to work from.

Here is the code that I have that currently is working just takes along time to complete.

Import-Csv $TransFile -Header 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 | ForEach-Object {
    if ($_.6 -match "PRCXX") {
    $_.9 = $_.10
    } 
$_ | Export-Csv test2.csv -NoTypeInformation -Append -Delimiter ","
}

Thanks for any help that you can provide.

2
  • 1
    The most time consumptive is a disk operation. Move the | Export-Csv test2.csv -NoTypeInformation -Append -Delimiter "," out from ForEach-Object loop body. Commented Aug 20, 2019 at 18:40
  • Wow that fixed it. Thanks for pointing that out can't believe I didn't catch that. Commented Aug 20, 2019 at 18:47

1 Answer 1

2

Thanks to JosefZ this is fixed. Moved the Export to occur outside the loop and that cleared it up.

Import-Csv $TransFile -Header 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 | ForEach-Object {
    if ($_.6 -match "PRCXX") {
    $_.9 = $_.10
    } 
$_ 
} | Export-Csv test2.csv -NoTypeInformation -Append -Delimiter ","
Sign up to request clarification or add additional context in comments.

1 Comment

Please consider marking your own answer as accepted. See this page for an explanation of why this is important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.