结合使用多种的.csv文件合并成一个.csv文件行明智使用批处理文件 [英] Combining multiple .csv files into one .csv file line wise using a batch file

查看:247
本文介绍了结合使用多种的.csv文件合并成一个.csv文件行明智使用批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况我需要做四个CSV文件合并成一个CSV文件。这是很容易,如果我只是想将它们添加一个接一个,但我需要让他们并排CSV文件。我知道,这四个文件具有相同数量的条目(在1000项范围内)。我一直在下面的code这适用于小文件,但对于长文件非常低效的。是否有完成相同的任务变得更容易的方式?

I have a situation where I need to combine four CSV files into one CSV file. This is easy if i just wanted to add them one after the other, but I need to get them to be side by side in the CSV file. I know that all four files have the same number of entries (in the 1000 entry range). I have been working on the following code which works for small files but is extremely inefficient for long files. Is there any easier way to accomplish the same task?

@echo off
setlocal enabledelayedexpansion
Set R=1
Set T=1
Set Y=1
Set U=1
for /f %%a in (test1.txt) do (
    set I=!R!
    for /f %%b in (test2.txt) do (
        set J=!T!
        for /f %%c in (test3.txt) do (
            set K=!Y!
            for /f %%d in (test4.txt) do (
                set L=!U!
                If !I!==!J! If !J!==!K! If !K!==!L! echo %%a,%%b,%%c,%%d >> TestComplete.txt
                Set /a U=U+1
            ) 
            Set U=1
            Set /a Y=Y+1
        )
        Set Y=1
        Set /a T=T+1
    )
    Set T=1
    Set /a R=R+1
)

注:我知道,code我在粘贴使用.txt文件,而不是.csv格式文件。我想什么工作的人会为其他工作。

Note: I know that the code I have pasted in is using .txt files and not .csv files. I assume what will work for one will work for the other.

此外,以上code似乎工作的伟大,只要文件很小。我(当然)麻烦时,这些文件(在这种情况下test1.txt的)大约有1000多行文本。

Again, The above code seems to work great as long as the files are small. I have trouble (of course) when the files (in this case test1.txt) have around 1000 lines of text.

推荐答案

GnuWin ,一个港口的集GNU(类似Unix的)工具到Windows,包含粘贴这不正是你想要的。不过,我感觉到你preFER本机Windows的答案,我想出了一个,但它不是pretty。与<起始href=\"http://stackoverflow.com/questions/7031687/merge-2-txt-files-in-a-single-tab-delimited-file-in-batch/7037534#7037534\">@walid2mi's聪明的解决方案和分拆下来到coljoi​​n.bat:

GnuWin, a collection of ports of GNU (Unix like) tools to Windows, contains paste which does exactly what you want. However, I sense you'd prefer a native Windows answer and I've come up with one, but it's not pretty. Starting with @walid2mi's clever solution and striping it down to coljoin.bat:

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (%1) do (
    set /p line=
    echo !line!, %%a
)

您可以加入四个文件有:

You can join four files with:

type a.txt | coljoin b.txt | coljoin c.txt | coljoin d.txt > TestComplete.txt

要求应与文件长度增加而线性增加,避免您的解决方案的经济增长大幅减速的时间。然而,这种解决方案的故障模式是不好,当所有的文件不具有线的数目相等。用户通常会提示输入丢失线条。

Time required should increase linearly with file length, avoiding the drastic slowdown of your solution. However the failure mode of this solution is not good when all the files do not have an equal number of lines. The user will usually be prompted to enter missing lines.

这篇关于结合使用多种的.csv文件合并成一个.csv文件行明智使用批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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