mercredi 21 février 2018

powershell replace command if line starts with a specific character

I have a text file that I would like to read and do some replacements using powershell only if the line starts with a specific character.

SAy i want to change all the dash (-) to an 'x' if and only if the line starts with a y.

I tried using the command

(Get-Content trial.log2) | Foreach-Object {$_ -replace "-", 'x'} | Set-Content trial.log2

However, it actually replaces all occurrences of the dash, not only for the line the starts with a y.

Any help is appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire