segunda-feira, 2 de janeiro de 2023

Powershell - How can I inject a newline by replacement with this script? (excelente explicação)

 You can put enter image description here in place of the space between the replacement string where you want the newline (CRLF) to be placed to get the expected result—backtick "r" backtick "n" (see below).

This only requires that one small change to the existing script and it'll work as expected per your description and example output results.


Script

@echo off
setlocal enableextensions disabledelayedexpansion

set search=Old string
set replace=New`r`nstring
set textFile=Test.txt

:PowerShell
SET PSScript=%temp%\~tmpStrRplc.ps1
ECHO (Get-Content "%~dp0%textFile%").replace("%search%", "%replace%") ^| Set-Content "%~dp0%textFile%">"%PSScript%"

SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
Powershell -ExecutionPolicy Bypass -Command "& '%PSScript%'"
PAUSE
EXIT

Note: Be sure to not put any spaces between the two strings in the replace= variable to ensure there is no trailing or leading spaces in the new string with the newline as you need.

enter image description here


Results

Before

Old string

After

New
String

Further Resources



fonte: https://superuser.com/questions/1298701/how-can-i-inject-a-newline-by-replacement-with-this-script

0 comentários: