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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

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

#1 Post by BoQsc » 16 May 2020 06:52

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


penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

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

#2 Post by penpen » 16 May 2020 08:44

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

Post Reply