Page 1 of 1

Substitute a Word with a word and a new-line.

Posted: 16 May 2020 06:52
by BoQsc
I'd like that in the variable SET "substitute=quiethaha" the value quiethaha would be split in two lines:

Code: Select all

quiet
haha
I'm quite sure it is possible. But I can't really remember on how.


search-replace.cmd

Code: Select all

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET "word=quiet"
SET "substitute=quiethaha"
FOR /F "delims=" %%a IN (E:\isolinux\txt.cfg) DO (
    SET "line=%%a"

    CALL SET "line=%%line:%word%=%substitute%%%"
    ECHO !line!
)

pause

E:\isolinux\txt.cfg

Code: Select all

default live
label live
  menu label ^Try Ubuntu without installing
  kernel /casper/vmlinuz
  append  file=/cdrom/preseed/ubuntu.seed initrd=/casper/initrd quiet splash ---
label live-nomodeset
  menu label ^Try Ubuntu without installing (safe graphics)
  kernel /casper/vmlinuz
  append  file=/cdrom/preseed/ubuntu.seed initrd=/casper/initrd quiet splash nomodeset ---
label live-install
  menu label ^Install Ubuntu
  kernel /casper/vmlinuz
  append  file=/cdrom/preseed/ubuntu.seed only-ubiquity initrd=/casper/initrd quiet splash ---
label live-install-nomodeset
  menu label ^Install Ubuntu (safe graphics)
  kernel /casper/vmlinuz
  append  file=/cdrom/preseed/ubuntu.seed only-ubiquity initrd=/casper/initrd quiet splash nomodeset ---
label memtest
  menu label Test ^memory
  kernel /install/mt86plus
label hd
  menu label ^Boot from first hard disk
  localboot 0x80


Re: Substitute a Word with a word and a new-line.

Posted: 16 May 2020 08:44
by penpen
The newlinecharacter is a little bit tricky; you can't use it within a replacement string in percentage environment variable expansion, but you could use it with for variable expansion:

Code: Select all

@ECHO OFF
SETLOCAL enableExtensions ENABLEDELAYEDEXPANSION

set "word=quiet"
set nl=^


set "substitute=quiethaha"
set "line=!substitute!"
for %%a in ("!nl!") do (
	set "line=!line:%word%=%word%%%~a!"
	echo(!line!
)

pause
goto :eof
penpen