China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-07 00:58
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How can DOS merge matching lines from two files View 698 Replies 4
Original Poster Posted 2007-12-27 18:14 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 71
Posts 59
Joined 2007-12-27 17:53
18-year member
UID 106966
Gender Male
Status Offline
If there are two files, 1.txt and 2.txt
Contents of 1.txt:
a1
b1
c1
d1
e1
Contents of 2.txt:
a2
b2
c2
d2
e2
Merge them into 3.txt
[1]a1[2]a2[3]
[1]b1[2]b2[3]
[1]c1[2]c2[3]
[1]d1[2]d2[3]
[1]e1[2]e2[3]
Floor 2 Posted 2007-12-27 18:17 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 71
Posts 59
Joined 2007-12-27 17:53
18-year member
UID 106966
Gender Male
Status Offline
Experts, please help me out... thanks...
Floor 3 Posted 2007-12-27 18:19 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 71
Posts 59
Joined 2007-12-27 17:53
18-year member
UID 106966
Gender Male
Status Offline
I originally had one txt file and split it into three files
for /f %%i in (a1.txt,a3.txt,a5.txt) do echo %%i>>a1.txt
Now I want to merge them in the above format....
Floor 4 Posted 2007-12-27 23:13 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline
@echo off & SetLocal EnableDelayedExpansion

for %%a in (1.txt 2.txt) do set /a num+=1&call :context %%a !num!

for /l %%a in (1,1,%maxline%) do (
set lineno=%%a
>>3.txt echo:!file_1_%%a!!file_2_%%a!
)
echo Processing complete&pause & start notepad 3.txt&exit /b 0

:context
set line=0
for /f "delims=" %%a in (%1) do (
set /a line+=1
set file_%2_!line!=%%a
)
if not defined maxline set maxline=%line%
goto :EOF
Floor 5 Posted 2007-12-28 09:56 ·  中国 广东 广州 天河区 电信
初级用户
★★
Credits 71
Posts 59
Joined 2007-12-27 17:53
18-year member
UID 106966
Gender Male
Status Offline
Many thanks.. moderator lxmxn
Forum Jump: