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

后端 未结 5 1663
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 08:42

file 1

A
B
C

file 2

B
C
D

file1 + file2 =

A
B
C
D

Is it possible to do

5条回答
  •  不思量自难忘°
    2020-12-10 09:18

    The solution below assume that both input files are sorted in ascending order using the same order of IF command's comparison operators and that does not contain empty lines.

    @echo off
    setlocal EnableDelayedExpansion
    
    set "lastLine=ÿ"
    for /L %%i in (1,1,10) do set "lastLine=!lastLine!!lastLine!"
    
    < file1.txt (
       for /F "delims=" %%a in (file2.txt) do (
          set "line2=%%a"
          if not defined line1 set /P line1=
          if "!line1!" lss "!line2!" call :advanceLine1
          if "!line1!" equ "!line2!" (
             echo !line1!
             set "line1="
          ) else (
             echo !line2!
          )
       )
    )
    if "!line1!" neq "%lastLine%" echo !line1!
    goto :EOF
    
    
    :advanceLine1
    echo !line1!
    set "line1="
    set /P line1=
    if not defined line1 set "line1=%lastLine%"
    if "!line1!" lss "!line2!" goto advanceLine1
    exit /B
    

提交回复
热议问题