I want to remove duplicated lines from a text file but keeping only 1st occurence using windows batch scripting.
I tried many ways but it's long and not efficient.
would you please help ?
I want to remove duplicated lines from a text file but keeping only 1st occurence using windows batch scripting.
I tried many ways but it's long and not efficient.
would you please help ?
The Batch file below do what you want:
@echo off
setlocal EnableDelayedExpansion
set "prevLine="
for /F "delims=" %%a in (theFile.txt) do (
if "%%a" neq "!prevLine!" (
echo %%a
set "prevLine=%%a"
)
)
If you need a more efficient method, try this Batch-JScript hybrid script that is developed as a filter, that is, similar to Unix uniq
program. Save it with .bat extension, like uniq.bat
:
@if (@CodeSection == @Batch) @then
@CScript //nologo //E:JScript "%~F0" & goto :EOF
@end
var line, prevLine = "";
while ( ! WScript.Stdin.AtEndOfStream ) {
line = WScript.Stdin.ReadLine();
if ( line != prevLine ) {
WScript.Stdout.WriteLine(line);
prevLine = line;
}
}
Both programs were copied from this post.