Batch file 如何从文本文件中读取一行,将其拆分为两行,然后将这两行输出到另一个文本文件?
我想要一个批处理代码,它可以执行以下操作:Batch file 如何从文本文件中读取一行,将其拆分为两行,然后将这两行输出到另一个文本文件?,batch-file,cmd,batch-processing,Batch File,Cmd,Batch Processing,我想要一个批处理代码,它可以执行以下操作: 将文本文件的所有内容加载到变量中。内容只有一行 然后把那条线分成两部分 将这两部分作为新行放入另一个文本文件中 假设我的内容是: Bill Gates was born in 1955. <--When was Bill Gates born?--> 句子的句法: xx yy <--xxy yyz --> 请注意,句子可以改变。我只是举了个例子。句子必须分成两部分,第一部分在之前这是一个简单的方法: @echo off ty
Bill Gates was born in 1955. <--When was Bill Gates born?-->
句子的句法:
xx yy <--xxy yyz -->
请注意,句子可以改变。我只是举了个例子。句子必须分成两部分,第一部分在
之前这是一个简单的方法:
@echo off
type "file.txt" | repl "(.*)(<.*)" "$1\r\n$2" x >"newfile.txt"
@echo关闭
键入“file.txt”| repl”(.*)(对于答案来说太容易了:
set /p line=<in.txt
for /f "tokens=1,2 delims=<" %%i in ("%line%") do (
echo %%i>out.txt
echo ^<%%j>>out.txt
)
set/p line=out.txt
echo^>out.txt
)
第一个选项,子字符串操作
@echo off
setlocal enableextensions disabledelayedexpansion
rem read the first line of input file
< file.txt set /p "line1="
rem extract the second line of output file
set "line2=%line1:*<--=<--%"
rem extract the first line of output file
set "line=%line2%"
:loop
set "line1=%line1:~0,-1%"
set "line=%line:~0,-1%"
if defined line goto loop
rem generate output file
setlocal enabledelayedexpansion
( echo !line1!
echo !line2!
) > result.txt
endlocal
@echo关闭
setlocal enableextensions disabledelayedexpansion
rem读取输入文件的第一行
设置“line2=%line1:*这听起来确实像是家庭作业。哈哈,不,一点也不。我住在印度。在这里,他们实际上什么都不教编程。所以我试着自己学习一些东西。如果你愿意,你可以在谷歌上搜索“第十届CBSE印度计算机课程”。)它不是我想要的。但它确实是你描述的。我想把这个句子分成两个不同的句子。你的代码只是把句子拉出来。我只是再次测试以确认它。只是再次尝试(从上面复制代码):它根据请求将一行文件拆分为两行文件。我遗漏了什么?所有三个答案(foxidrive的、MC的和我的)都产生了非常相同的输出。如果foxi的回答让你比其他人更快乐,那没关系。但是如果你说,其他代码不起作用,我们想知道为什么输出会不同,以及如何不同。它不符合我的要求。@PawanKartik,对我来说,根据你的数据测试,这(两个代码)符合你的要求。如果这不是你想要的,也许你应该改变问题,以反映你想要的是什么,或者,至少指出得到的结果与你期望得到的结果有什么不同。我想把这个句子分成两个不同的句子。你的代码只是把句子拉出来。我只是再次测试以确认它。@PawanKartik,对不起,但对我来说它是有效的。请测试我在答案中包含的最后一个代码。这是相同的代码,但它将生成输入文件,读取并显示输出。文件可能存在编码问题。
set /p line=<in.txt
for /f "tokens=1,2 delims=<" %%i in ("%line%") do (
echo %%i>out.txt
echo ^<%%j>>out.txt
)
@echo off
setlocal enableextensions disabledelayedexpansion
rem read the first line of input file
< file.txt set /p "line1="
rem extract the second line of output file
set "line2=%line1:*<--=<--%"
rem extract the first line of output file
set "line=%line2%"
:loop
set "line1=%line1:~0,-1%"
set "line=%line:~0,-1%"
if defined line goto loop
rem generate output file
setlocal enabledelayedexpansion
( echo !line1!
echo !line2!
) > result.txt
endlocal
@echo off
setlocal enableextensions disabledelayedexpansion
rem read the first line of input file
< file.txt set /p "line="
rem extract and output the lines
for /f "tokens=1,2 delims=|" %%a in ("%line:<--=|<--%") do (
echo %%a
echo %%b
) > result.txt
@echo off
setlocal enableextensions disabledelayedexpansion
rem prepare the sample file to be readed
>file.txt echo Bill Gates was born in 1955. ^<--When was Bill Gates born?--^>
rem read the first line of input file
< file.txt set /p "line="
rem extract and output the lines
for /f "tokens=1,2 delims=|" %%a in ("%line:<--=|<--%") do (
echo %%a
echo %%b
) > result.txt
rem show the result
type result.txt