Search code examples
batch-fileduplicatesfindstr

Look for number of duplicates and copy the line in batch


In File_1 I have:

Word_1;ger
Word_1;gr
Word_1;greber
Word_1;gaerfsd
Word_2;gerbhge
Word_2;tgbzrfvd
Word_3;gzfdfdc

I want to calculate the number of duplicates of each first column of each line then, depending on the number of duplicates(one or different than one), I will copy paste them to two different files.

File_2 will contain:

Word_3;gzfdfdc

File_3 will contain:

Word_1;ger
Word_1;gr
Word_1;greber
Word_1;gaerfsd
Word_2;gerbhge
Word_2;tgbzrfvd

Here is the code that I wrote:

setlocal EnableDelayedExpansion
    (for /f "tokens=1-2 delims=;" %%a in (File_1) do (
        set current_line=%%a
        if "!current_line!" NEQ "!previous_line!" (
            for /f %%C in ('Find /C %%a ^< File_1) do (
                set Count=%%C
                if "!Count!==1" (
                        findstr %%a File_1 >>File_2
                )
                if not "!Count!==1" (
                        findstr %%a File_1 >>File_3
                )
            )
        )
        set previous_line=!current_line!
    )

It doesn't seem to work. Any Help ?


Solution

  • removed an unneeded variable, corrected an erroneous if syntax and added some quotes for "best practice". Seems to do exactly, what you intend:

    setlocal EnableDelayedExpansion
    (for /f "tokens=1-2 delims=;" %%a in (File_1.txt) do (
      if "%%a" NEQ "!previous_line!" (
        for /f %%C in ('Find /C "%%a" ^< File_1.txt') do (
          if "%%C"=="1" (
            findstr "%%a" File_1.txt >>File_2.txt
          ) else (
            findstr "%%a" File_1.txt >>File_3.txt
          )
        )
      )
      set "previous_line=%%a"
    ))