2013-11-20 36 views
0

我想合併2個csv文件。例如,csv1文件包含前六列,而csv2文件包含(前6列爲空)第七,第八,第九列。在合併的csv中,我需要原樣(前6列--first csv和7,8,9- ---第二CSV).kindly幫我提前合併2個csv文件,因爲它是

+2

請把CSV數據類型的例子:它含有特殊的一批符號如。 ()!?我沒有看到批量並行讀取來自兩個文件的數據的方法。所以需要預先閱讀一個文件。我建議使用VB或C#代替您的任務。 –

+0

沒有特殊批次symbols.i試過類型* .csv> merged.csv.but它複製csv2文件在第7,8,9列,但不是csv1 ...我dono爲什麼? – user2855659

+0

發佈這兩個文件和你想要的結果的一個簡短的例子,請! – Aacini

回答

0

由於這是更難只用批處理命令的事,如果你會考慮使用VBS或Perl,然後調用腳本在您的批處理文件,它會使它更容易。

0

我假設文件不會太大,無法將它們加載到內存中,並且每個csv文件中不超過999999行。腳本結束時,CMD將立即退出。

@echo off 
setlocal enabledelayedexpansion 
set file1Content= 
set file2Content= 

rem Read first file in "file1Content" variable, split lines by slash 
for /f %%a in (Document1.csv) do (
    set file1Content=!file1Content!/%%a 
) 
rem Remove first leading slash 
set file1Content=!file1Content:~1! 

rem Read second file in "file2Content" variable, split lines by slash 
for /f %%a in (Document2.csv) do (
    set file2Content=!file2Content!/%%a 
) 
rem Remove first leading slash 
set file2Content=!file2Content:~1! 

set rest1= 
set rest2= 
for /L %%a in (1,1,999999) do (
    set item1= 
    set item2= 
    rem Take first line from "!file1Content!" 
    for /f "tokens=1,* delims=/" %%x in ("!file1Content!") do (
     set item1=%%x 
     set rest1=%%y 
    ) 
    rem Removing first line (the one we read) from content1 
    set file1Content=!rest1! 
    rem Exit if there's no content 
    if "!item1!"=="" exit 

    rem Take first line from "!file2Content!" 
    for /f "tokens=1,* delims=/" %%x in ("!file2Content!") do (
     set item2=%%x 
     set rest2=%%y 
    ) 
    rem Removing first line (the one we read) from content2 
    set file2Content=!rest2! 
    rem Exit if there's no content 
    if "!item2!"=="" exit 
    echo !item1!,!item2!>>joined.csv  
) 
2

讓我們假設file1.csv具有這種格式線:

col1,col2,col3,col4,col5,col6 

和file2.csv具有這種格式線:

,,,,,,col7,col8,col9 

批處理文件下創建合併文件與文件1中的col1..col6和文件2中的col7..col9;它假定兩個文件具有相同的行數,並且在任何文件中都沒有空行。

@echo off 
setlocal EnableDelayedExpansion 

< file2.csv (
    for /F "delims=" %%a in (file1.csv) do (
     set /P line2= 
     echo %%a,!line2:~6! 
    ) 
) > merged.csv 
+0

它完美無瑕。 –

0
Here is the command for doing it the easy way 

    cut -c7- y.txt | paste -d',' x.txt - > z.txt 

    The 'cut' command cuts off the first 6 characters of file y.txt 
    The 'paste' command puts together file x.txt with that modified y.txt 
    using comma for a separator and puts the results in file z.txt 

    for example: 

    file x.txt 
    col1,col2,col3,col4,col5,col6 
    col1,col2,col3,col4,col5,col6 
    col1,col2,col3,col4,col5,col6 
    col1,col2,col3,col4,col5,col6 

    file y.txt 
    ,,,,,,col7,col8,col9 
    ,,,,,,col7,col8,col9 
    ,,,,,,col7,col8,col9 
    ,,,,,,col7,col8,col9 

    cut -c7- y.txt | paste -d',' x.txt - > z.txt 

    file z.txt 
    col1,col2,col3,col4,col5,col6,col7,col8,col9 
    col1,col2,col3,col4,col5,col6,col7,col8,col9 
    col1,col2,col3,col4,col5,col6,col7,col8,col9 
    col1,col2,col3,col4,col5,col6,col7,col8,col9