0

I am trying to replace lines of text with a part of the text not always being a static value.

I have found that the method described in the post below is working except in one case where the line variable I am trying to change is containing an asterisk (*).

How to replace an entire line in text file by only knowing a portion of the line?

Otherwise if in testing I remove the asterisk it works. How can I have it ignore that there is what it thinks is a wildcard in the variable and just treat the * as more text?

$line = Get-Content D:\tmp\test.txt | Select-String TextString | Select-Object -ExpandProperty Line
Get-Content D:\tmp\test.txt | ForEach-Object {$_ -replace $line,'TextString = VALUEIWANT'} | Set-Content D:\tmp\TEXTNEW.txt
  • 2
    it sounds like the regex engine is choking on the reserved character `*`. if so, you can escape that character and it will be treated as a literal asterisk. the easiest way to escape chars in a string [for me] is to use the builtin dotnet `[regex]::Escape($YourStringHere)` method to generate an escaped version of the string. – Lee_Dailey Oct 31 '18 at 16:22
  • Awesome that worked. Thank You very much!! – Brent Chickey Oct 31 '18 at 17:59
  • you are most welcome! glad to help a tad ... [*grin*] – Lee_Dailey Oct 31 '18 at 18:01

0 Answers0