文本文件并排合并无法通过Bat文件工作

时间:2015-03-12 04:38:18

标签: csv batch-file merge

要并排合并两个csv文件,我使用了以下代码:

@echo off
setlocal EnableDelayedExpansion

< ..\Data\2.csv (
   for /F "delims=" %%a in (..\Data\1.csv) do (
      set /P line2=
      echo %%a,!line2!
   )
) > ..\Data\input.csv

它在两个小的csv文件上运行良好,例如:

File 1
------
a     
b     
File 2
------
apple 
ball 

但是当我尝试合并这两个csv文件时, 文件1:

source,hcid,fname,lname,dob,gender,relTpCd,Rel
WGS20,902A07430,Subs,Performancel,1965-07-01,F,SCRBR,Subscriber
WGS20,686T08408,Abdullah,Wilson,1965-01-01,F,SCRBR,Subscriber
WGS20,265A06410,Aewae,Asfwafc,1974-11-18,M,SCRBR,Subscriber

文件2:

SLangCd,SLang,SLangDesc,WLangC
100,SPOKEN LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100,SPOKEN LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100449,SPOKEN LANGUAGE,Undetermined,100449,WRITTEN LANGUAGE,Undetermined

我正在获得这样的合并文件(文件2的行单独打印:

source,hcid,fname,lname,dob,gender,relTpCd,Rel,SLangCd,SLang,SLangDesc,WLangC
WGS20,902A07430,Subs,Performancel,1965-07-01,F,SCRBR,Subscriber,100,SPOKEN     LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100,SPOKEN LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100449,SPOKEN LANGUAGE,Undetermined,100449,WRITTEN LANGUAGE,Undetermined
WGS20,686T08408,Abdullah,Wilson,1965-01-01,F,SCRBR,Subscriber,100,SPOKEN     LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100,SPOKEN LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100449,SPOKEN LANGUAGE,Undetermined,100449,WRITTEN LANGUAGE,Undetermined
WGS20,265A06410,Aewae,Asfwafc,1974-11-18,M,SCRBR,Subscriber,100,SPOKEN LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100,SPOKEN LANGUAGE,English,100449,WRITTEN LANGUAGE,Undetermined
100449,SPOKEN LANGUAGE,Undetermined,100449,WRITTEN LANGUAGE,Undetermined

1 个答案:

答案 0 :(得分:2)

您的文件有一个LF(换行符,0x0A字符)作为行终止,而不是Windows中的CRLF(回车符,&#39; 0x0D 0x0A`字符)标准行终止符。

虽然for /f命令能够处理它,但重定向的set /p将检索到第一个CR或1021个字符的所有内容,首先会发生什么。

您需要处理(至少)2.csv文件,例如

type 2.csv | find /v "" > 2.csv.tmp

将生成具有正确行终止的临时文件。这是您应该用作重定向输入的文件。