(编辑:jimmy 日期: 2024/11/10 浏览:2)
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