杰网资源 Design By www.escxy.com

a.txt内容如下:
123
456
789
123
123
789

获取重复的行,并且重复的行只保留一次,处理后得到b.txt内容如下:
123
789

方法1:

@echo off
REM 缺点1:无法处理特别大的文件
REM 缺点2:需要使用文件中没有出现的字符来标记变量(本例中使用的是下划线)
setlocal
for /f "delims=" %%i in (a.txt) do (
  set /a _%%i+=1
)
(for /f "tokens=1-2 delims=_=" %%i in ('set _') do (
  if %%j gtr 1 (
    echo,%%i
  )
))>b.txt
endlocal

方法2:

@echo off
setlocal enabledelayedexpansion
set "PriLine="
set "DupNum=1"
(for /f "delims=" %%i in ('sort a.txt') do (
  if "!PriLine!" equ "%%i" (
    set /a DupNum+=1
  ) else (
    if !DupNum! gtr 1 (
      echo,!PriLine!
    )
    set DupNum=1
  )
  set "PriLine=%%i"
))>b.txt
if !DupNum! gtr 1 (
  b.txt echo,!PriLine!
)

方法3:

gawk "{a[$0]++}END{for(i in a)if(a[i]>1)print i}" a.txt > b.txt

方法4:(更加简洁)

gawk "a[$0]++" a.txt>b.txt

方法5:

@echo off
for /f "tokens=*" %%i in (a.txt) do set #%%i=%%i
(for /f "tokens=2 delims==" %%i in ('set #') do echo %%i)>b.txt

标签:
批处理过滤重复行,批处理删除重复行

杰网资源 Design By www.escxy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
杰网资源 Design By www.escxy.com