2

I have the below powershell file and need to replace multiple commands. Is it possible to replace the text containing parenthesis and quotation marks with the -replace command?

 $original_file = 'D:\Prtr\tr.lst'
 $destination_file =  'D:\Prtr\pr.iso'
 (gc $original_file -raw) | ForEach-Object {
 $_ -replace 'TL_FORCE(11,"PRX",0,100)', '$(FREE_LQQ)(1,2)' `
 -replace 'TL_FORCE(11,"PRX",0,200)', '$(FREE_LQQ)(2,2)' `
 -replace 'TL_FORCE(11,"PRX",0,200)', '$(FREE_LQQ)(3,2)' `
 } | Set-Content $destination_file

The way I have it currently written doesn't replace anything. I tried using a double backslash to isolate the quotation marks but without success.

paul33
  • 25
  • 3
  • There's not enough information in this question to help you. Please provide a sample of your source file. – Maximilian Burszley Oct 07 '20 at 16:28
  • 1
    I mean on the face of it you just escape parenthesis ( eg. `\(` and `\)` ) to match them, the double quotes `"` would be replaced just by using them since you are using single quotes `'` to define your regex here. Simple as that. So if the literal text you are looking to match is `TL_FORCE(11,"PRX",0,200)` you would just use this instead: `TL_FORCE\(11,"PRX",0,200\)` as your match – Ben Personick Oct 07 '20 at 16:48
  • Thank you Ben! This answers the question...just using a backslash TL_FORCE\(11,"PRX",0,200\). I didn't understand that the double quotes are already being taken care of by '. – paul33 Oct 07 '20 at 17:32
  • The alternative to character-individual escaping with `\ ` is to use the `[regex]::Escape()` method or to use the `.Replace()` method of string objects, which performs _literal_ substring searches and replacements, albeit case-_sensitively_ by default). See [this answer](https://stackoverflow.com/a/60068470/45375) to the linked post. – mklement0 Oct 07 '20 at 18:14

0 Answers0