CSV文件,更改单元格值

时间:2019-03-21 08:11:28

标签: powershell

https://i.stack.imgur.com/IjGvZ.png

productNumber                 productName       active description
-------------                 -----------       ------ -----------
productNumber                 productName       active description
1A100110001                   product1          TRUE   laptop
1A100110002                   product2          TRUE   laptop
1A100110003                   product3          TRUE   laptop
1A100110004                   product4          TRUE   laptop
1A100110005                   product5          TRUE   laptop
1A100110006                   product6          TRUE   laptop
1A100110007                   product7          TRUE   laptop
1A100110008                   product8          TRUE   laptop
1A100110009,product9,FALSE,PC

我想将productName(product2)的值更改为(product fornonox 2)

我尝试了以下代码,但没有用:

$products_fortnox = import-csv -Path .\products_Fortnox_output.csv 
$products_fortnox | % {

if ($_.productNumber -eq "1A100110002") {

$_.productName = 'product fortnox 2'
} 
}

$products_fortnox | export-csv .\products_Fortnox_output.csv -NoTypeInformation

1 个答案:

答案 0 :(得分:0)

尝试这个:

$source  = '.\products_Fortnox_output.csv '
$dest   = '.\products_Fortnox_output_new.csv '

[IO.File]::ReadAllText($filepath) -replace '(?m)^(1A100110002,)(prod.*)(,.*)$', '$1product fortnox 2$3' | Out-File $dest