我需要一个批处理文件来组合两个文本文件。我的档案是:
file1.txt
1
2
3
4
file2.txt
A
B
C
D
E
F
G
H
I
J
K
L
我需要像这样的混合文件
mix.txt
1
A
B
C
D
2
E
F
G
H
3
I
J
K
L
注意:对于文件1的每一行,我需要文件2.1->1-4,2->5-8,3->9-12中的4行。
我尝试了这个程序(bat文件解决方案),但不知道,如何修改它以获得结果与我的要求。
@echo off
set f1=file1.txt
set f2=file2.txt
set outfile=mix.txt
type nul>%outfile%
(
for /f "delims=" %%a in (%f1%) do (
setlocal enabledelayedexpansion
set /p line=
echo(%%a!line!>>%outfile%
endlocal
)
)<%f2%
pause
发布于 2022-03-25 17:01:27
你需要一些线路计数器:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Improved quoted `set` syntax:
set "f1=file1.txt"
set "f2=file2.txt"
set "outfile=mix.txt"
set "quotient=4"
rem // Redirect the whole block once:
< "%f1%" > "%outfile%" (
rem // Temporarily precede each line with a line number:
for /f "delims=" %%a in ('findstr /N "^" "%f2%"') do (
rem // Get modulo of the line number:
set "item=%%a" & set /A "num=(item+quotient-1)%%quotient"
setlocal EnableDelayedExpansion
rem // Return a line from the other file only if modulo is zero:
if !num! equ 0 (
set "line=" & set /P line=""
echo(!line!
)
rem // Return current line with the line number prefix removed:
echo(!item:*:=!
endlocal
)
)
endlocal
pause
file2.txt
的长度决定了迭代次数。
https://stackoverflow.com/questions/71619159
复制相似问题