Home > Software engineering >  Powershell- how to get a one-line command to find a string and replace it by adding 2 new lines
Powershell- how to get a one-line command to find a string and replace it by adding 2 new lines

Time:02-10

Is it possible in Powershell to get a single line command to find a string and replace it by adding 2 new lines with a different value leaving the beginning and end of the file unchanged?

(Get-Content -Path C:\test.txt) -replace 'aaa=0','aaa=20 \n\r g=11111 \n\r g=2222' | Set-Content -Path C:\test.txt)

source file:
test=txt
dds=1aa
ab=test
aaa=0
bbb=4444

target file:
test=txt
dds=1aa
aaa=20
g=11111
g=2222

bbb=4444

CodePudding user response:

To insert CRLF without adding literal newlines in the source code, use the `r and `n escape sequences in an expandable string:

... -replace 'aaa=0',"aaa=20 `r`ng=11111 `r`ng=2222"
  • Related