find same values(duplicate) or vlookup formula

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

find same values(duplicate) or vlookup formula

#1 Post by santhosh » 16 Aug 2010 19:02

hi,
i have two text files, i want to find common values in that(duplicate) like vlookup formula which we use in excel sheet.
example:-
sample1.txt contains
33
445
667
22
45
97
67
57
86
8585
789
sample2.txt
565
46534
656
87
35
33
86
output in sample3.txt:-
33
86

aGerman
Expert
Posts: 4656
Joined: 22 Jan 2010 18:01
Location: Germany

Re: find same values(duplicate) or vlookup formula

#2 Post by aGerman » 17 Aug 2010 04:49

Try this

Code: Select all

>"sample3.txt" type nul
for /f "usebackq delims=" %%A in ("sample1.txt") do (
  for /f "usebackq delims=" %%a in ("sample2.txt") do (
    if "%%A"=="%%a" >>"sample3.txt" echo\%%A
  )
)


Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: find same values(duplicate) or vlookup formula

#3 Post by ghostmachine4 » 18 Aug 2010 08:34

download gawk for windows, then use this one liner

Code: Select all

c:\test>  gawk.exe  "FNR==NR{a[$1];next}($1 in a)" file1 file2
33
86


this has the advantage of using memory which is faster than double for loops from aGerman's solution.

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: find same values(duplicate) or vlookup formula

#4 Post by santhosh » 20 Aug 2010 00:17

hi friends

thanks for both of u.it works good.
In my office pc i dont have authority to install any package,already i told this to agerman that dont use any vbsctipt r gawk.


Anyway ghostmachine, thanks for your idea i will use in my home pc.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: find same values(duplicate) or vlookup formula

#5 Post by ghostmachine4 » 20 Aug 2010 01:41

santhosh wrote:hi friends

thanks for both of u.it works good.
In my office pc i dont have authority to install any package,already i told this to agerman that dont use any vbsctipt r gawk.


you can download from your home, its just an exe file. Put it inside your portable disk or USB drive, then bring to workplace to use.

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: find same values(duplicate) or vlookup formula

#6 Post by santhosh » 08 Sep 2010 01:10

hi ghostmachine,


c:\test> gawk.exe "FNR==NR{a[$1];next}($1 in a)" file1 file2

now i want to print values which is not same.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: find same values(duplicate) or vlookup formula

#7 Post by ghostmachine4 » 08 Sep 2010 01:28

santhosh wrote:hi ghostmachine,


c:\test> gawk.exe "FNR==NR{a[$1];next}($1 in a)" file1 file2

now i want to print values which is not same.


use a "!" to invert the condition

Code: Select all

gawk.exe  "FNR==NR{a[$1];next}( !( $1 in a) )" file1 file2


santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: find same values(duplicate) or vlookup formula

#8 Post by santhosh » 08 Sep 2010 06:24

Thanks ghostmachine.one more doubt.

want to compare only 2nd and 3rd column (both column should be equal)example:-
File1 contains:-
sat,005,87
sat1,09,95
sat3,40,98
sat4,50,99
sat5,40,97

File2 contains:-
ggg,010,55
hth,68,95
sat,42,97
sat6,40,98

output should be File3:-
sat,40,98


Thanks in advance

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: find same values(duplicate) or vlookup formula

#9 Post by ghostmachine4 » 08 Sep 2010 06:37

and how is that sat and not sat3 and sat6 ?

santhosh
Posts: 41
Joined: 02 Aug 2010 05:10

Re: find same values(duplicate) or vlookup formula

#10 Post by santhosh » 07 Oct 2010 04:07

yes sorry ghost,
i missed that line
output is :-
sat3,40,98
sat6,40,98

Post Reply