使用批处理文件并排合并多个csv文件 [英] Merge several csv file side by side using batch file

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

问题描述

我正在寻找两个将多个csv文件合并为一个的文件.但是,我需要将它们合并,以使列并排不会继续. 我的文件每次都有两列,通过提取每个文件的第二列并将其复制到另一个文件中,我将得到一个csv文件,因此我将得到一个带有x列的文件(第二个).

I was looking two merge several csv files into one. However I need them merging so that the columns are side by side not continuing on. My files have two columns each time and I would have one csv files by extracting the 2nd column on each files and copying into another one so I will have a file with x columns (the second one).

例如

File1

A B
1 2
1 2
1 2

File2

A C
1 3
1 3
1 3

Filex

A X
1 x
1 x
1 x

结果

B C X
2 3 x
2 3 x
2 3 x

我发现了这个问题:并排使用合并csv文件批处理文件

但是它仅用于两个文件,并且不提取任何内容.

but it's just for two files and it doesn't extract anything.

谢谢.

推荐答案

@ECHO OFF
SETLOCAL enabledelayedexpansion
:: remove variables starting $
FOR  /F "delims==" %%a In ('set $ 2^>Nul') DO SET "%%a="
SET /a $count=0
FOR %%a IN (q28850167*.txt) DO (
 FOR  /f "tokens=1,2*delims=: " %%b IN ('findstr /n /r "^" "%%a"') DO (
  SET $%%b=!$%%b! %%d
  IF !$count! LSS %%b SET /a $count=%%b
 )
)
(
FOR /L %%a IN (1,1,%$count%) DO ECHO(!$%%a:~1!
)>newfile.txt


GOTO :EOF

我使用了名为q28850167*.txt的文件,其中包含用于测试的数据.
产生newfile.txt

I used files named q28850167*.txt containing your data for my testing.
Produces newfile.txt

清除所有以$

对于每个与掩码匹配的文件,请通过findstr/n处理每一行,以将number :应用于每一行的开头.选择: Space 作为分隔符的第一和第三+列,并追加到变量$linenumber跟踪$count中的最高行号.

For each file matching the mask, process each line through findstr/n to apply number : to the start of each line. Select the first and third+ columns using : and Space as separators and append to variable $linenumber Keep track of the highest line number in $count.

然后只需从存储的数据中复制每一行.

Then simply reproduce each line from the stored data.

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

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