使用Windows批处理从文本文件中删除重复行 [英] Remove Duplicate Lines from Text File using Windows batching

查看:600
本文介绍了使用Windows批处理从文本文件中删除重复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文本文件中删除重复的行,但使用Windows批处理脚本仅保留第一次出现的情况.

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.

请您帮忙吗?

推荐答案

下面的批处理文件可以满足您的要求:

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"
   )
)

如果您需要更有效的方法,请尝试以 filter 开发的Batch-JScript混合脚本,即类似于Unix uniq程序的脚本.用.bat扩展名保存,例如uniq.bat:

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;
   }
}

两个程序均从复制而来这篇文章.

这篇关于使用Windows批处理从文本文件中删除重复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆