0

Let say in a text file, I have:

Planets.txt

Mars
Earth
Jupiter
Saturn
Mars
Uranus
Jupiter
Pluto

How can I remove all the occurrences and left the words appear once in a text file?

Output_Planets.txt (Occurrences: Mars, Jupiter)

Mars
Earth
Jupiter
Saturn
Uranus
Pluto

I have no idea how to do that... Thanks.

WhatWhereWhen
  • 473
  • 1
  • 5
  • 6
  • 1
    [`for` command](http://ss64.com/nt/for.html) might help. Combine `for /F` against `Planets.txt` file contents with `for %G in (Mars, Jupiter) do @echo %G` (note that `@echo` is here a sample placeholder). Read [ask] and How to create a [mcve]. – JosefZ Nov 11 '15 at 07:55

1 Answers1

1
@ECHO OFF
SETLOCAL
:: establish a zero-byte output file
SET "newfile=u:\newfile.txt"
COPY /y nul "%newfile%" 2>NUL >nul
FOR /f "delims=" %%a IN (q33645665.txt) DO (
 FINDSTR /x /i /l /c:"%%a" "%newfile%" >NUL
 IF ERRORLEVEL 1 >>"%newfile%" ECHO %%a
)

TYPE "%newfile%"

GOTO :EOF

I used a file named q33645665.txt containing your data for my testing.

Produces u:\newfile.txt

FINDSTR will set errorlevel to 0 if a string matching %%a is found in the new file. /xforces the match to be on the entire line, /i specifies that the match be case-insensitive. /l makes the match literal, not a regular expression and /c: preceding a string specifies that the string provided is one element, not a set of strings so that a string containing separators liek spaces can be matched.

Magoo
  • 77,302
  • 8
  • 62
  • 84