简体   繁体   中英

Looking for help optimizing a Powershell script to parse through a CSV line by line

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.

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 ","

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM