0

I have a csv-file with data like this:

"89216865";"89216865";"Alternator";"PowerMax";"4543"
"MG 149";"MG 149";"MAHLE Alternator";"MAHLE AM GmbH";"4543"
"MG 258";"MG 258";"MAHLE Alternator";"MAHLE AM GmbH";"4543"
"MS 222";"MS 222";"MAHLE Starter";"MAHLE AM GmbH";"4543"
"MS 241";"MS 241";"MAHLE Starter";"MAHLE AM GmbH";"4543"
"MS 29";"MS 29";"MAHLE Starter";"MAHLE AM GmbH";"4543"
"MS 386";"MS 386";"MAHLE Starter";"MAHLE AM GmbH";"4543"
"MS 645";"MS 645";"MAHLE Starter";"MAHLE AM GmbH";"4543"
"MS 230";"MS 230";"MAHLE Starter";"MAHLE AM GmbH";"4543"
"3300216";"3300216";"Alternator OE";"PowerMax";"4543"
"9213171";"9213171";"Alternator";"PowerMax";"4543"
"8212676";"8212676";"Starter";"PowerMax";"4543"
"9214266";"9214266";"Alternator";"PowerMax";"4543"

I need to replace the string "4543" with "287" in every row where "MAHLE AM GmbH" appears.

I have tried this.

Get-Content "C:\Users\SvcBI\Documents\Autodoc\Autodoc_Temp.csv" | 
Foreach-Object ($_ -match "MAHLE AM GmbH") {-replace "4543", "287"} | 
Set-Content C:\Users\SvcBI\Documents\Autodoc\POWERMAX_DK_2.csv

Please help /Kim

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
Kim Jensen
  • 15
  • 5

1 Answers1

0

It looks like you are using Powershell.

One way to do it could be reading the lines, and if the line contains the value, the perform the replacement.

See this page for the meaning of $_

Get-Content "C:\Users\SvcBI\Documents\Autodoc\Autodoc_Temp.csv" | ForEach-Object {
    if($_.Contains("MAHLE AM GmbH")) {
        $_.Replace("4543", "287") 
    } else { $_ }
} | Set-Content C:\Users\SvcBI\Documents\Autodoc\POWERMAX_DK_2.csv
The fourth bird
  • 154,723
  • 16
  • 55
  • 70