a.txt内容如下:
123
456
789
123
123
789
获取重复的行,并且重复的行只保留一次,处理后得到b.txt内容如下:
123
789
方法1:
@echo offREM 缺点1:无法处理特别大的文件REM 缺点2:需要使用文件中没有出现的字符来标记变量(本例中使用的是下划线)setlocalfor /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.txtendlocal
方法2:
@echo offsetlocal enabledelayedexpansionset "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.txtif !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 offfor /f "tokens=*" %%i in (a.txt) do set #%%i=%%i(for /f "tokens=2 delims==" %%i in ('set #') do echo %%i)>b.txt
新闻热点
疑难解答