Question about replacing one specific character in a value

With the following I try to select a pattern like .1;ENM and then I want to replace the dot with a comma.

Code example

Get-Content .\import.csv |

Select-String -pattern “.\d;ENM”

| ForEach-Object {$_.matches.value -replace ‘.’,‘,’}

I select an object and select the value (which is a string) but the whole value is replaced with the comma so the output I get is , instead of the wanted ,1;ENM. Can anybody point me in the right direction?


The dot “.” is a special charachter for regex. So you have to escape it if you want to find a literal one. :wink:

Get-Content .\import.csv |
Select-String -pattern '\.\d;ENM' | 
ForEach-Object {$_.matches.value -replace '\.',','}

When you post code or error messages or sample data or console output format it as code using the code tags “PRE” you find in the text view of the post editor, please. Thanks in advance.

Also, you can use parentheses to create a match group then replace.

Get-Content .\import.csv | ForEach-Object {$_ -replace '(\.)(\d;ENM)',',$2'}

Do you want to change the whole line?

(get-content import.csv) -replace '\.',',' | set-content import2.csv

When in doubt, [regex]::escape



Thank you soo much! Immediate replies, not only the solution but also some explainations and worthwhile background.

And from a learning point of view, since I really struggled with this and when I read your comments it really makes sense so it is etched in my mind and really learned.

So thanks community, what a wonderfull forum this is!