Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to join two text files, removing duplicates, in Windows

Tags:

batch-file

file 1

A
B
C

file 2

B
C
D

file1 + file2 =

A
B
C
D

Is it possible to do using cmd.exe?

like image 631
Victor Avatar asked Nov 29 '22 15:11

Victor


1 Answers

If you can affort to use a case insensitive comparison, and if you know that none of the lines are longer than 511 bytes (127 for XP), then you can use the following:

@echo off
copy file1.txt merge.txt >nul
findstr /lvxig:file1.txt file2.txt >>merge.txt
type merge.txt

For an explanation of the restrictions, see What are the undocumented features and limitations of the Windows FINDSTR command?.

like image 68
dbenham Avatar answered Dec 06 '22 10:12

dbenham