使用批处理文件并排合并多个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.