3

We have 2 Unicode files. One of them contains lines that are missing in another file. Like so:

1. 
2. bbbbbbbbbbbbbbbbb
3. 
4. ddddddddddddddddddddd
5. eeeeeeeeeeeeeeeeeeeeeeee


1. aaaaaaaaaaaaaa
2. 
3. ccccccccccccccccc
4.
5.

We want to merge them into third file that will contain all lines:

1. aaaaaaaaaaaaaa
2. bbbbbbbbbbbbbbbbb
3. ccccccccccccccccc
4. ddddddddddddddddddddd
5. eeeeeeeeeeeeeeeeeeeeeeee

Notes:
a,b,c,d,e - can be any text.
line numbers are just for illustration purposes, they are not present in actual files.

I created this question with "batch-file" tag, but I am open to any suggestions about how to achieve this. Of course better not to involve something like C++

4

1 回答 1

1

假设没有行以冒号开头,首先我们将两个文件中的每一行读入两个数组,包括空行 - 对此有一个特殊的 hack,因为正常 for 跳过空行。然后将两个数组中具有相同索引的元素连接起来,并输出到 results.txt 中:

setlocal EnableDelayedExpansion

set i=0
for /f "tokens=1* delims=:" %%A in ('type "file1.txt" ^| findstr /n "^"') do (
    set /A i+=1
    set arr1[!i!]=%%B
)

set i=0
for /f "tokens=1* delims=:" %%A in ('type "file2.txt" ^| findstr /n "^"') do (
    set /A i+=1
    set arr2[!i!]=%%B
)

for /L %%i in (1,1,%i%) do echo.!arr1[%%i]!!arr2[%%i]!>> result.txt
于 2013-04-30T20:38:42.517 回答