Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

target file:

test=txt

dds=1aa

aaa=20

g=11111

g=2222


bbb=4444

>Solution :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading