Using Windows/DOS shell/batch commands, how do I take a file and only keep unique lines?

前端 未结 8 1299
刺人心
刺人心 2020-12-06 02:56

Say I have a file like:

apple
pear
lemon
lemon
pear
orange
lemon

How do I make it so that I only keep the unique lines, so I get:



        
8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 03:17

    The SORT command in Windows 10 does have an undocumented switch to remove duplicate lines.

    SORT /UNIQ File.txt /O Fileout.TXT
    

    But a more bullet proof option with a pure batch file you could use the following.

    @echo off
    setlocal disableDelayedExpansion
    set "file=MyFileName.txt"
    set "sorted=%file%.sorted"
    set "deduped=%file%.deduped"
    ::Define a variable containing a linefeed character
    set LF=^
    
    
    ::The 2 blank lines above are critical, do not remove
    sort "%file%" >"%sorted%"
    >"%deduped%" (
      set "prev="
      for /f usebackq^ eol^=^%LF%%LF%^ delims^= %%A in ("%sorted%") do (
        set "ln=%%A"
        setlocal enableDelayedExpansion
        if /i "!ln!" neq "!prev!" (
          endlocal
          (echo %%A)
          set "prev=%%A"
        ) else endlocal
      )
    )
    >nul move /y "%deduped%" "%file%"
    del "%sorted%"
    

提交回复
热议问题