ktraos
ktraos

Reputation: 127

how to compare two files on names and remove the common name and corresponding value from first file

I have two .properties files as follows

first.properties                    second.properties
-----------------                   ---------------------
firstname=firstvalue                fourthname=100100
secondname=secondvalue              sixthname=200200
thirdname=thirdvalue                nineththname=ninethvalue
fourthname=fourthvalue              tenthname=tenthvalue
fifthname=fifthvalue
sixthname=sixthvalue
seventhname=seventhvalue

i want to compare two files with the names and need to remove common name and corresponding value from first.properties. The output file should be as

third.properties.
------------------ 
firstname=firstvalue                
secondname=secondvalue              
thirdname=thirdvalue            
fifthname=fifthvalue
seventhname=seventhvalue

i used the following code,but it is giving Cartesian product scenario.can you please help me out to achieve the above.

for /F "tokens=1,2 delims==" %%E in (first.properties) do (
    for /F "tokens=1,2 delims==" %%G in (second.properties) do (
    if "%%E" NEQ "%%G" echo %%E=%%F>>!HOME!\Properties\third.properties
    )
    )

Upvotes: 1

Views: 76

Answers (2)

Aacini
Aacini

Reputation: 67216

The Batch file below does not create temporary files nor use external commands (like findstr.exe), so it run faster.

@echo off
setlocal EnableDelayedExpansion

rem Create list of names in second file
set "secondNames=/"
for /F "delims==" %%a in (second.properties) do set "secondNames=!secondNames!%%a/"

rem Copy properties of first file that does not appear in second one
(for /F "tokens=1* delims==" %%a in (first.properties) do (
   if "!secondNames:/%%a/=!" equ "%secondNames%" (
      echo %%a=%%b
   )
)) > third.properties

I used a slash to delimit property names. If this character may appear in the names, just select a different one in the program.

Previous solution eliminate any exclamation mark from the files; this detail may be fixed, if needed.

Upvotes: 0

Endoro
Endoro

Reputation: 37569

try this:

@echo off
(for /f "delims==" %%i in (second.properties) do echo(%%i.*)>temp.properties
findstr /rvg:temp.properties first.properties>third.properties
del temp.properties

..output in third.properties is:

firstname=firstvalue
secondname=secondvalue
thirdname=thirdvalue
fifthname=fifthvalue
seventhname=seventhvalue

at the request of the OP I add a (much slower) solution without a temp file:

@echo off
(for /f "tokens=1,2delims==" %%i in (first.properties) do findstr /r "%%i.*" second.properties >nul||echo(%%i=%%j)>third.properties
type third.properties

Upvotes: 1

Related Questions