Windows批处理:同时遍历多张文件中的行 [英] Windows batch: Iterate over mulitple files lines at the same time
问题描述
是否有可能遍历多个文件在同一时间线?我需要输出由加盟两个不同的文件中的行的文件(并添加一些额外的字)。
Is it possible to iterate over the lines of multiple files at the same time? I need to output a file by joining the lines in two different files (and add some extra words).
我知道CMD命令:
FOR %variable IN (set) DO command [command-parameters]
但如果你需要同时迭代两个文件没有帮助。
更多precisely,我需要以下内容:
But that does not help if you need to iterate over two files at the same time. More precisely, I need to the following:
While the first file is not ended
line1 <- READ a line from the first file
line2 <- READ a line from the second file
WRITE on a third file line1 + line2
我正在寻找一种方法来完成I'vve previously在 DOS批处理文件描述。谢谢!
推荐答案
我知道的3个基本技巧,从中可以得出大量的杂种。
I know of 3 basic techniques, from which a large number of hybrids could be derived.
选项1 (这个选项是非常相似的WMZ溶液)
Option 1 (this option is very similar to the wmz solution)
用FOR / F读取文件1,并用FOR / F第二与SKIP选项读取文件2。文件2必须在调用的子程序被读取,以使环能不破坏文件1环被打破。
Read file 1 with a FOR /F and read file 2 with a second FOR /F with a SKIP option. File 2 must be read in a CALLed subroutine so that the loop can be broken without breaking the file 1 loop.
限制:
- 空白行会导致行不同步的。空行被包括在跳过计数,但不被FOR / F读出。
- 与
开头的行;
不会,因为默认EOL选项的阅读。这可以通过在必要时EOL设置到一个新的行字符来解决。请参见 HOW TO:FOR / F禁用EOF或使用引号作为DELIM - 行被限制在8191字节的最大长度。
- Blank lines will cause lines to get out of synch. Blank lines are included in the skip count but are not read by FOR /F.
- Lines beginning with
;
will not be read because of the default EOL option. This can be solved by setting EOL to a new line character if necessary. See HOW TO: FOR /F Disabling EOF or using a quote as delim - Lines are limited to a maximum length of 8191 bytes.
这个选项是慢,因为CALL和由于第二个文件必须每行被读取一次。
This option is slow because of the CALL and because the 2nd file must be read once for each line.
的编辑 - 固定为code还是输出线,如果第二个文件结束早的
@echo off
setlocal disableDelayedExpansion
set "file1=a.txt"
set "file2=b.txt"
set "out=out.txt"
set /a cnt=0
set "skip="
>"%out%" (
for /f "usebackq delims=" %%A in ("%file1%") do (
set "found="
call :readFile2
if not defined found (echo %%A - )
)
)
type "%out%"
exit /b
:readFile2
for /f "usebackq %skip% delims=" %%B in ("%file2%") do (
set found=1
echo %%A - %%B"
goto :break
)
:break
set /a cnt+=1
set "skip=skip=%cnt%"
exit /b
选项2
这个选项可以使用FINDSTR至preFIX与行号后面跟着一个冒号的每一行解决空行的问题。 FINDSTR用于只读文件从2第n行,所以没有必要打出来的第二个循环。
This option solves the blank line problem by using FINDSTR to prefix each line with the line number followed by a colon. FINDSTR is used to read only the nth line from file 2, so no need to break out of the 2nd loop.
限制:
- 领导冒号将从线被剥离。这种限制可以与额外code被消除,但会使其更复杂和更慢。
- 行被限制在8191字节的最大长度。
这个选项比选项1更慢
的编辑 - 固定为code还是输出线,如果第二个文件结束早的
@echo off
setlocal disableDelayedExpansion
set "file1=a.txt"
set "file2=b.txt"
set "out=out.txt"
>"%file2%.tmp" findstr /n "^" "%file2%"
>"%out%" (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" "%file1%"') do (
set "found="
for /f "tokens=1* delims=:" %%a in ('findstr "^%%A:" "%file2%.tmp"') do (
set found=1
echo %%B - %%b
)
if not defined found (echo %%B - )
)
)
del "%file2%.tmp"
type "%out%"
选项3
使用SET / P找这两个文件。 FIND用于获取的行数的计数中文件1,因为设置/ P能不能告诉文件的空白线和端部之间的差异。
Use SET /P to read both files. FIND is used to get a count of the number of lines in file 1 because SET /P cannot tell the difference between a blank line and end of file.
这个选项消除很大的局限性和复杂性,但引入了其自身的局限性。
This option eliminates a lot of limitations and complexity, but introduces its own limitations.
限制:
- 线必须使用
&LT的视窗风格行终止; CR&GT;&LT; LF&GT;
。 Unix样式&LT; LF方式&gt;
将无法正常工作 - 线被限制在1021字节
- 尾随控制字符从每行剥离。
- Lines must use Windows style line terminators of
<CR><LF>
. Unix style<LF>
will not work. - lines are limited to 1021 bytes
- trailing control characters are stripped from each line.
这个选项是迄今为止最快的。它是preferred只要限制是可以接受的。
This option is by far the fastest. It is preferred as long as the limitations are acceptable.
@echo off
setlocal enableDelayedExpansion
set "file1=a.txt"
set "file2=b.txt"
set "out=out.txt"
for /f %%N in ('type "%file1%"^|find /c /v ""') do set "cnt=%%N"
>"%out%" 9<"%file1%" <"%file2%" (
for /l %%N in (1 1 %cnt%) do (
set "ln1="
set "ln2="
<&9 set /p "ln1="
set /p "ln2="
echo !ln1! - !ln2!
)
)
type "%out%"
这篇关于Windows批处理:同时遍历多张文件中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!