批处理文件:如何连接文本文件中的每一行? [英] Batch Files: How to concatenate each line in a text file?
问题描述
我有一个文本文件,每行有文本。我想要能够把每一行在一个长线与一个空格。所以,如果文本文件有:
Bob
Jack
Sam
我想要的结果是
Bob Jack Sam
以下是我正在开发的两种方法,括号中的任何内容意味着我知道语法是完全错误的;我只是把它放在那里,以显示我的思想过程。评论部分只是我的实验,我已经离开他们,以防任何人想评论他们会做什么/为什么他们做他们做什么。
方法1: / p>
@echo off
SETLOCAL EnableDelayedExpansion
SET count = 1
FOR / Ftokens = * delims = usebackq%% x IN(afile.txt)DO(
SET var!count!= %% x
for %% a in(!count!)do(
!var %% a!=!var %% a!&
echo!var %% a!
)
SET / a count =!count! $ b echo!count!
)
:: echo!var1! !var2! !var3!
startfirefox.exe!var %% a!-1
ENDLOCAL
:: echoendlocal%var1%%var2%%var3%方法2:
@echo off
SETLOCAL EnableDelayedExpansion
SET count = 1
FOR / Ftokens = * delims = usebackq%% x IN(afile.txt)DO (
SET var!count!= %% x
call echo %% var!count!%%
SET / a count =!count!+1
echo!count!
)
:: echo!var1! !var2! !var3!
startfirefox.exe ^
[i = 1]
[for i to!count! do(]
call echo %% var!count!%%&&^
ENDLOCAL
:: echoendlocal% var1%%var2%%var3%
解决方案有三个值,可以直接从文件中检索它们
< input.txt(
set / p line1 =
set / pline2 =
set / pline3 =
)
setvar =%line1%%line2%%line3%
echo(%var%
如果不知道值的数量, $ for / f
命令读取行并将内容连接到变量。
@echo off
setlocal enableextensions enabledelayedexpansion
setvar =
for / fusebackq delims =%% a in(input.txt) do setvar =!var!%% a
echo(%var%
但是如果数据可以包含惊叹号,延迟扩展状态将删除它们(和它们包围的文本)。要避免它,可以使用
@echo off
setlocal enableextensions disabledelayedexpansion
setvar =
for / fusebackq delims =%% a in(input.txt)do(
setlocal enabledelayedexpansion
for / ftokens = * delims =¬ %% b in(¬!var!)do endlocal& setvar = %% b %% a
)
echo(%var%
其中 setlocal / endlocal
和内部 c>
@echo off
setlocal enableextensions disableelayedexpansion
for / fdelims = %% a in('
< nul cmd / q / cfor / fusebackq delims =%% z in(input.txt^)do(set / p。= %% z^)
')do setvar = %% a
echo(%var%
它运行一个 cmd
实例将输入行作为只有一个输出行( < nul set / p
用于输出没有换行符的数据,因此所有数据都在同一行)。这包裹在中
以检索变量中的输出
I have a text file with text on each line. I would like to be able to put each line in one long line along with a space. So, if the text file has:
Bob
Jack
Sam
I want the result to be
Bob Jack Sam
Below are two methods that I am working on but I am stuck. Anything in brackets [] means that I know the syntax is completely wrong; I only put it there to show my thought process. The commented sections are just me experimenting and I have left them in case anyone wants to comment on what they would do / why they do what they do.
Method 1:
@echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
for %%a in (!count!) do (
!var%%a! = !var%%a! & " "
echo !var%%a!
)
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe !var%%a!-1
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
Method 2:
@echo off
SETLOCAL EnableDelayedExpansion
SET count=1
FOR /F "tokens=* delims= usebackq" %%x IN ("afile.txt") DO (
SET var!count!=%%x
call echo %%var!count!%%
SET /a count=!count!+1
echo !count!
)
::echo !var1! !var2! !var3!
start "" firefox.exe ^
[i = 1]
[for i to !count! do (]
call echo %%var!count!%% & " " & " "^
ENDLOCAL
::echo "endlocal" %var1% %var2% %var3%
解决方案 If you only have three values, you can directly retrieve them from the file
< input.txt (
set /p "line1="
set /p "line2="
set /p "line3="
)
set "var=%line1% %line2% %line3%"
echo("%var%"
If you don't know the number of values, use a for /f
command to read the lines and concatenate the contents into a variable.
@echo off
setlocal enableextensions enabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do set "var=!var!%%a "
echo("%var%"
But if the data can contain exclamation signs, the delayed expansion state will remove them (and the text surounded by them). To avoid it, this can be used
@echo off
setlocal enableextensions disabledelayedexpansion
set "var="
for /f "usebackq delims=" %%a in ("input.txt") do (
setlocal enabledelayedexpansion
for /f "tokens=* delims=¬" %%b in ("¬!var!") do endlocal & set "var=%%b%%a "
)
echo("%var%"
where the setlocal/endlocal
and the inner for
are used to avoid problems with !
character
Or you can try something like this
@echo off
setlocal enableextensions disabledelayedexpansion
for /f "delims=" %%a in ('
"<nul cmd /q /c "for /f "usebackq delims=" %%z in ("input.txt"^) do (set /p ".=%%z "^)""
') do set "var=%%a"
echo("%var%"
It runs a cmd
instance to output the input lines as only one output line (<nul set /p
is used to ouput the data without line feeds, so all data is in the same line). This is wrapped in a for
to retrieve the output inside a variable
这篇关于批处理文件:如何连接文本文件中的每一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!