We want to replace multiple instances of the variable id=*, with a certain pattern, like id=1234.
I already made this Powershell script (and prefer to keep using Powershell as solution):
$line = Get-Content C:\test.txt | Select-String "id=" | Select-Object -ExpandProperty Line
$content = Get-Content C:\test.txt
$content | ForEach {$_ -replace $line,"id=1234"} | Set-Content C:\test.txt
Get-Content C:\test.txt
This works, as long as there's only 1 instance of id=..., when a file contains multiple instances of id=... there is no replacement step performed at all.
The input file is similar to:
text over here
id=1
text over here: id={123456}
text
id=number1
id=#3 text
id=3+3 text
which should result in:
text over here
id=1234
text over here: id=1234
text
id=1234
id=1234 text
id=1234 text
(Get-Content C:\test.txt) | ForEach {$_ -replace "id=.*","id=1234"}(gc C:\test.txt) -replace '(?<=id=)[^\s]+','1234'