【问题标题】:Merge 2 csv files as it is按原样合并 2 个 csv 文件
【发布时间】:2013-11-20 15:11:54
【问题描述】:

我想合并 2 个 csv 文件。例如 csv1 文件包含前六列,csv2 文件包含(前 6 列为空)第 7、8、9 列。在合并的 csv 中,我需要原样(前 6 列 --first csv 和 7、8、9- ---第二个csv)。请帮助我

提前致谢

【问题讨论】:

  • 请提供 csv 数据类型的示例:它是否包含特殊的批处理符号,例如。 ()!.?我看不到从这两个文件中批量读取数据的方法。所以需要预读一个文件。我建议改为使用 VB 或 C# 来完成您的任务。
  • 没有特殊的批处理符号。我尝试了 type*.csv>merged.csv。但是它正在复制第 7、8、9 列中的 csv2 文件而不是 csv1...我不知道为什么?
  • 张贴一个简短的例子,说明这两个文件和你想要的结果,拜托!
  • 另外我假设两个 csv 文件的行数相同。并且 csv 中没有空行。

标签: batch-file csv merge


【解决方案1】:

假设 file1.csv 有这种格式的行:

col1,col2,col3,col4,col5,col6

并且该 file2.csv 具有这种格式的行:

,,,,,,col7,col8,col9

下面的批处理文件使用来自 file1 的 col1..col6 和来自 file2 的 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

【讨论】:

    【解决方案2】:

    我假设文件不会太大而无法将它们加载到内存中,并且每个 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    
    )
    

    【讨论】:

      【解决方案3】:
          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
      

      【讨论】:

        猜你喜欢
        • 2018-10-31
        • 1970-01-01
        • 1970-01-01
        • 2016-05-01
        • 1970-01-01
        • 2015-09-18
        • 1970-01-01
        • 2010-10-24
        • 1970-01-01
        相关资源
        最近更新 更多