Home > front end >  Replace all minus numbers with 0 Powershell
Replace all minus numbers with 0 Powershell

Time:09-29

How can I replace all minus numbers with 0 in a particular column in PowerShell

I only want to examine the quality column on my CSV

Example Data:

Name      Colour    Quality
Apple     RED       5
Pear      Green     4
Plum      Purple    -3
Melon     Yellow    -1

CodePudding user response:

should do what you want:

$data = import-csv [path]
$data  | ForEach-Object {
    $_.quality = $_.quality -replace  "-\d","0"
}

The regex pattern for replace specifies to replace minus followed by a digit (\d = 0-9)

result:

Name  Colour Quality
----  ------ -------
Apple RED    5
Pear  Green  4
Plum  Purple 0
Melon Yellow 0
  • Related