数据文件1:
帐号、部门、地点、金额
10000,DEPT_100,LOC_101,1000
20000,DEPT_200,LOC_200,2000
30000,DEPT_300,LOC_300,3000
40000,DEPT_400,LOC_400,4000
数据文件2:
帐号、部门、地点、金额
10000,DEPT_100,LOC_100,1000
20000,DEPT_200,LOC_200,2000
30000,DEPT_300,LOC_300,3000
40000,DEPT_400,LOC_400,4000
所需输出(数据文件 1 - 数据文件 2 - 差异:)
10000、DEPT_100、LOC_101、1000 - 10000、DEPT_100、LOC_100、1000 - 差异:LOC_100
电流输出:
10000,DEPT_100,LOC_100,1000 - 10000DEPT_100LOC_1001000,
@echo off
setlocal enabledelayedexpansion
set "file1=input_file1.csv"
set "file2=input_file2.csv"
set "output_file=differences_output.csv"
:: Concatenate columns in both files
for /f "usebackq tokens=1-4,* delims=," %%a in ("%file1%") do (
set "line=%%a,%%b,%%c,%%d - %%a%%b%%c%%d,%%e"
echo !line!>>temp_file1.csv
)
for /f "usebackq tokens=1-4,* delims=," %%a in ("%file2%") do (
set "line=%%a,%%b,%%c,%%d - %%a%%b%%c%%d,%%e"
echo !line!>>temp_file2.csv
)
:: Perform VLOOKUP
findstr /v /g:temp_file1.csv temp_file2.csv > %output_file%
:: Loop through records in the output file and display differences
for /f "usebackq tokens=1-5,* delims=-," %%a in ("%output_file%") do (
echo Record: %%a,%%b,%%c,%%d,%%e - Difference: %%f
)
:: Cleanup temporary files
del temp_file1.csv
del temp_file2.csv
echo Done. Differences written to %output_file%
嗯...我认为你应该首先定义“文件差异”的含义...
但是,如果您有两个文件,其行数“相同”且逐一匹配,并且想知道第二个文件中的哪些字段未出现在第一个文件中,则此解决方案可以解决问题:
@echo off
setlocal EnableDelayedExpansion
< test2.txt (
for /F "delims=" %%a in (test1.txt) do (
set /P "line2="
set "diffs= !line2:,= ! "
for %%b in (%%a) do set "diffs=!diffs: %%b = !"
if "!diffs!" neq " " (
echo %%a - !line2! - Difference:!diffs!
)
)
)
输出:
10000,DEPT_100,LOC_101,1000 - 10000,DEPT_100,LOC_100,1000 - Difference: LOC_100
请注意,有一些未定义的细节或限制。例如:行中不能有空格、星号、问号或感叹号。准确地说,每行必须有 4 个字段。还有其他人...