0

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 ?

R.Omar
  • 141
  • 1
  • 2
  • 11
  • `%count%` should be `!count!` – Stephan Aug 19 '18 at 10:45
  • What's the reason @Stephan? It doesn't work either – R.Omar Aug 19 '18 at 10:53
  • Reason id [delayed expansion](https://stackoverflow.com/a/30284028/2152082). Thought, you knew, because you are already using delayed expansion correctly with `current_line`. Didn't test your code. Correct the `count` issue and then post any occuring errormessages. – Stephan Aug 19 '18 at 10:56

2 Answers2

1

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"
))
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • @Aacini ugh - I was sure, I took it out (therefore "removed an unneeded variable") - done; thanks for spotting. – Stephan Aug 20 '18 at 06:45
0

Although your (corrected) code works ok, it is pretty inefficient. You execute findstr command two times for each group of duplicates. If the input file is large, this method could take a lot of time.

You may get the same result with a single for /F pass over the file, with no use of findstr:

@echo off
setlocal EnableDelayedExpansion

del File_2.txt File_3.txt 2>NUL
set "last1="
set "moreThanOne="
for /f "tokens=1-2 delims=;" %%a in (File_1.txt) do (
   if "%%a" equ "!last1!" (
      >> file_3.txt echo !last1!;!last2!
      set "moreThanOne=1"
   ) else (
      if defined moreThanOne (
         >> file_3.txt echo !last1!;!last2!
      ) else if defined last1 (
         >> file_2.txt echo !last1!;!last2!
      )
      set "moreThanOne="
   )
   set "last1=%%a"
   set "last2=%%b"
)
if defined moreThanOne (
   >> file_3.txt echo !last1!;!last2!
) else (
   >> file_2.txt echo !last1!;!last2!
)
Aacini
  • 65,180
  • 12
  • 72
  • 108