Windows Batch Scripting: Collect unique occurences of a string in a file

batch-file, windows

Solution

FOR /F "tokens=1 delims=," %%i in (FILE) do ( find "%%i" "%temp%\u" >nul 2>&1 || <nul set/p=%%i,>> "%temp%\u")
type "%temp%\u"

what this does, is take the file line by line, grab everything before the first comma, and pass it in to the do. the do section of the loop attempts to find the string in a file containing the unique strings. if it does, than it returns true, and the second part is never evaluated. if it does not find it, than it writes the string followed by a comma to the file.

Problem

I have a file, in which the first string before the comma is some kind of identifier. Here is a sample: ``` A, bla, bla... B, bla, bla... A, bla, bla... C, bla, bla... ``` I need to parse a file to collect all unique occurences of this string. So, ideally, after processing I would have some kind of array `[A, B, C]`. The problem is that officially arrays are not supported in batch scripting. I know there are some workarounds, but the ones I checked out looked quite ugly. What I have so far, is something like this: ``` FOR /F "tokens=1 delims=, " %%i in (%FILE%) do ( echo %%i ) ``` This produces the output: ``` A B A C ``` How do I eliminate the duplicate occurences of a string? What would be the elegant way to achieve this? Please, share your thoughts, on how this problem could be solved.

Original source

Related problems