批处理脚本合并两个文件行到第三个文件 [英] Batch script to merge lines from two files into a third file

查看:155
本文介绍了批处理脚本合并两个文件行到第三个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件 A1 内容:

AA
VV
BB

文件 A2 内容:

DD
EE
FF

我要合并 A1 A2 如下成 A3 <内容/ code>,使 A3 是预期的数据:

I want to merge the contents of A1 and A2 as below into A3, so that the expected data in A3 is:

AADD
VVEE
BBFF

另外,在 A3预期的输出可能是:

AA is from DD
VV is from EE
BB is from FF

感谢您的帮助。我曾尝试和搜寻我发布之前,无法找到一个已经发布类似的东西有人...

Thanks for the help. I did try and search before I posted and could not find someone that has already posted something similar...

推荐答案

我们可以加载文件的内容到批处理变量数组所以它的每一个线条都可以在你希望的任何方式直接访问:

We can load the contents of the files into Batch variable arrays so each of its lines can be directly accessed in any way you wish:

@echo off
setlocal EnableDelayedExpansion

rem Load first file into A1 array:
set i=0
for /F "delims=" %%a in (A1.txt) do (
    set /A i+=1
    set A1[!i!]=%%a
)

rem Load second file into A2 array:
set i=0
for /F "delims=" %%a in (A2.txt) do (
    set /A i+=1
    set A2[!i!]=%%a
)

rem At this point, the number of lines is in %i% variable

rem Merge data from both files and create the third one:
for /L %%i in (1,1,%i%) do echo !A1[%%i]! is from !A2[%%i]!>> A3.txt

修改 替代解决方案

有另一种方式来做到这一点不使用批处理变量,以便它可以在任何大小的文件中使用,虽然它比较慢。我借用其解决方案中使用由安迪·莫里斯的方法:在两个文件中插入1号线,2合并这两个文件的酮,3-排序合并文件,以及4-合并线路组成一个同一直线上。下面的程序基本上是安迪的一个用,使得它更快(固定一个微妙的错误)几个小的修改。

There is another way to do it that don't use Batch variables so it can be used on files of any size, although it is slower. I borrowed the method used by Andy Morris in its solution: 1- Insert line numbers in both files, 2- Combine both files in one, 3- Sort the combined file, and 4- Merge groups of lines into one same line. The program below is basically Andy's one with several small modifications that made it faster (with a subtle error fixed).

@echo off
setlocal EnableDelayedExpansion

call :AddLineNumbers A1.txt A > Both.txt
call :AddLineNumbers A2.txt B >> Both.txt
sort Both.txt /O Sorted.txt
echo EOF: >> Sorted.txt
call :creatNewLines < Sorted.txt > Result.txt
goto :eof

:AddLineNumbers
findstr /n ^^ %1 > tem.tmp
for /f "tokens=1* delims=:" %%a in (tem.tmp) do (
    set /a lineNo=1000000+%%a
    echo !lineNo!%2:%%b
)
goto :eof

:creatNewLines
set /p lineA1=
for /f "tokens=1* delims=:" %%a in ("%lineA1%") do (
    if %%a == EOF goto :eof
    set /p dummy=%%b< nul
)
set /p lineA2=
for /f "tokens=1* delims=:" %%a in ("%lineA2%") do echo  is from %%b
goto creatNewLines

根据其内容SORT命令订单行。安迪的原始方法可以基于在线内容失败,因为行号后面的线是有序的,所以每个文件的线可以是不必要的。在这种方法中附加字符(A或B)的行号后添加,因此每个文件的行总是放置在正确的地方。

SORT command order lines based on its contents. Andy's original method may fail because after the line number the lines are ordered based on line contents, so the lines of each file may be misplaced. In this method an additional character (A or B) is added after the line number, so the lines of each file are always placed in the right place.

这篇关于批处理脚本合并两个文件行到第三个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆