蝙蝠将新行字符替换为“|”

时间:2014-11-12 07:54:14

标签: batch-file

我想将新行字符替换为" |"使用Window bat。

例如file1:

1
2
3

输出:

1|2|3

我试试这个蝙蝠:

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

但是,新行字符不是" \ r \ n"。如何获取新的行char表达式?

3 个答案:

答案 0 :(得分:5)

@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

答案 1 :(得分:0)

你可以稍微玩一下:

FOR /F "Usebackq Tokens=*" %%@ IN ("File.txt") DO (
<NUL Set /P "=%%@"
)

或者,/ p删除newLines:

C:\> echo Hello World
Hello World

C:\> echo|set /p=Hello World
Hello World

答案 2 :(得分:0)

对Krekkon的剧本进行了一些编辑:

@echo off

FOR /F "Usebackq Tokens=* delims=" %%# IN ("123.txt") DO (
  echo|set /p=%%#^^^|
)>>temp.file
move /y "temp.file" "123.txt"