Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bat replace new line char to "|"

Tags:

batch-file

I want to replace new line char to "|" use Window bat.

eg file1:

1
2
3

output:

1|2|3

I try this bat:

echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (123.txt) do (
set a=%%a
set a=!a:"\r\n"=^|!
for  %%b in ("!a!") do (
echo.%%~b>>1245.txt
))
pause

But, new line char is not "\r\n". How can I get the new line char expression ?

like image 515
Jerry Zhang Avatar asked Nov 12 '14 07:11

Jerry Zhang


1 Answers

@echo off
setlocal EnableDelayedExpansion

rem Initialize the output line
set "line="

rem Catenate all file lines in the same variable separated by "|"
for /F "delims=" %%a in (123.txt) do set "line=!line!|%%a"

rem Show final line, removing the leading "|"
echo !line:~1!>>1245.txt
like image 171
Aacini Avatar answered Oct 01 '22 09:10

Aacini