Batch - Replace a string with special characters

I would like to replace a line in a file using a batch file.

Line:

),(

      

And I want to replace it with:

),
(

      

I found several posts like this one: "how to replace-substrings-in-windows-batch-file" , but this example uses a dummy string with no special characters.

Thank!

EDIT

Context: I am using mysqldump

to fetch a database, and I would like each line of the insert command to be on a new line for more visibility.

I don't want to use --extended-insert=false

because it slows down re-installing the backup.

EDIT2

Example:

INSERT INTO `dummytable` (`dummycolumn`) VALUES (1),(2),(3);

      

I want this to be:

INSERT INTO `dummytable` (`dummycolumn`) VALUES (1),
(2),
(3);

      

+3


source to share


3 answers


Take a look at replacer.bat

call replacer.bat "e?C:\content.txt" "\u0022),(\u0022" "\u0022),\u000a(\u0022"

      

Edit without quotes:

call replacer.bat "e?C:\content.txt" "),(" "),\u000a("

      



window style

call replacer.bat "e?C:\content.txt" "),(" "),\u000D\u000a("

      

you can also check FindRepl and JRepl which are more complex tools

+1


source


This works for me:



@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET textFile=test.txt
TYPE NUL>tempOut.txt
FOR /F %%l IN (%textFile%) DO (
    SET line=%%l
    SET line=!line:"),("="),\n("!
    ECHO !line!>>tempOut.txt
)
MOVE /y tempOut.txt %textFile%

      

0


source


In batch mode, if the pattern is always the same, you can deal with tokens and delimises like this:

@echo off

(echo INSERT INTO `dummytable` (`dummycolumn`^) VALUES (1^),(2^),(3^);
)>%TEMP%\_file.tmp

(FOR /F "tokens=1,2,3* delims=," %%a IN (%TEMP%\_file.tmp) do (
    echo %%a,
    echo %%b,
    echo %%c
))>_output.txt
exit /b 0

      

output:

INSERT INTO `dummytable` (`dummycolumn`) VALUES (1),
(2),
(3);

      

Edit
I found another way from npocmaka's post at fooobar.com/questions/2265410 / ...

@echo off

setlocal disableDelayedExpansion
REM Creating a Newline variable (the two blank lines are required!)
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%

setlocal enableDelayedExpansion

for /f "delims=" %%a in (%TEMP%\_file.tmp) do (
    set "line=%%a"
    set line=!line:,(=,%NL%(!
    echo !line!
)

endlocal
endlocal
exit /b 0

      

0


source







All Articles