China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 15:41
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please clarify the specific content or context of "删掉所有重复行" in the batch processing. Because there are different implementation methods for deleting different types of duplicate lines in different scenarios. For example, if it is a text file with lines of text, the general idea is to read line by line, store it in a temporary variable or array, and then judge whether it has been stored before. If not, it is written to a new file. But the specific batch code needs to be further determined according to the actual situation. Please provide more details. View 2,259 Replies 23
Floor 16 Posted 2007-04-21 07:37 ·  中国 安徽 芜湖 电信
高级用户
★★
Credits 537
Posts 219
Joined 2006-10-31 21:08
19-year member
UID 69036
Gender Male
From 芜湖
Status Offline
for /f "delims=" %%i in (test.txt) do if not defined %%i set %%i=a&echo %%i
江湖远
碧空长
路茫茫

一个人漫无目的的奔跑,风,刺骨的冷....
Floor 17 Posted 2007-04-21 10:05 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Floor 18 Posted 2007-04-21 10:52 ·  中国 北京 宽捷网通信技术有限公司
初级用户
Credits 84
Posts 28
Joined 2006-05-03 22:36
20-year member
UID 54894
Gender Male
Status Offline
Floor 19 Posted 2007-09-26 14:20 ·  中国 湖北 武汉 中国科学院武汉分院
银牌会员
★★★
Credits 1,187
Posts 555
Joined 2006-12-21 07:35
19-year member
UID 74129
Gender Male
Status Offline
Originally posted by everest79 at 2007-4-21 04:41:
My this one can only delete the repeated lines

a
a
b
c
d

a
b
c
d
Cannot
a
a
b
c
d

b
c
d
src.txt is your original text, after execution, dst.txt is the filtered text. Switch the cmd to the directory where src.txt is saved, then execute the above line



Hahaha.... Finally found it
What I want is this
Give a thumbs up....
Floor 20 Posted 2007-09-26 22:22 ·  中国 广东 东莞 电信
银牌会员
★★★
Credits 1,284
Posts 538
Joined 2002-11-02 00:00
23-year member
UID 129
Gender Male
Status Offline
The code on floor 16 doesn't work
D:\DOCUME~1\ADMINI~1>for /f "delims=" %i in (test.txt) do if not defined %i set %i=a&echo %i

D:\DOCUME~1\ADMINI~1>if not defined aa set aa=a & echo aa

D:\DOCUME~1\ADMINI~1>if not defined bb set bb=a & echo bb

D:\DOCUME~1\ADMINI~1>if not defined cc set cc=a & echo cc

D:\DOCUME~1\ADMINI~1>if not defined dd set dd=a & echo dd

D:\DOCUME~1\ADMINI~1>if not defined bb set bb=a & echo bb

D:\DOCUME~1\ADMINI~1>if not defined cc set cc=a & echo cc

D:\DOCUME~1\ADMINI~1>if not defined ee set ee=a & echo ee

D:\DOCUME~1\ADMINI~1>
Floor 21 Posted 2007-09-26 23:26 ·  中国 北京 网联光通
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
::)

@echo off
setlocal enabledelayedexpansion
cd.>temp.txt
for /f "tokens=* delims=:" %%i in ('findstr /n ".*" test.txt') do (
echo %%i>>temp.txt
)

for /f "tokens=1* delims=:" %%m in (temp.txt) do (
for /f "tokens=1* delims=:" %%x in ('findstr /v /c:"%%m:" temp.txt') do (
if "%%n"=="%%y" (
set str=%%y !str!
)
)
)

findstr /v "!str!" test.txt
pause


[ Last edited by yoyodos on 2007-9-26 at 11:28 PM ]
Floor 22 Posted 2007-09-26 23:52 ·  中国 江苏 常州 武进区 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by chishingchan at 2007-9-26 22:22:
The code on floor 16 doesn't work
D:\DOCUME~1\ADMINI~1>for /f "delims=" %i in (test.txt) do if not defined %i set
%i=a&echo %i

D:\DOCUME~1\ADMINI~1>if not defined aa set aa=a & ech ...

The one on floor 16 should be okay!
@echo off
for /f "delims=" %%i in (test.txt) do if not defined %%i set %%i=a&echo %%i>>test2.txt
pause
Floor 23 Posted 2008-08-28 17:42 ·  中国 上海 闵行区 电信
新手上路
Credits 14
Posts 7
Joined 2008-08-25 10:53
17-year member
UID 124098
Gender Male
Status Offline
The sentence "set %%i=a" is used to set the variable represented by %%i to the value "a". In the context of this `for /f` loop, it's likely part of a process to initialize or assign a default value to each variable that is read from the `test.txt` file.
Floor 24 Posted 2008-08-28 18:05 ·  中国 上海 闵行区 电信
新手上路
Credits 14
Posts 7
Joined 2008-08-25 10:53
17-year member
UID 124098
Gender Male
Status Offline
Originally posted by bray at 2008-8-28 05:42 PM:

What is the use of `set %%i=a`?

Also, if you just want to delete adjacent duplicate lines, how to do it?
For example:
aaa
aaa
bbb
aaa
aaa
aaa
ccc
ddd
ccc

The desired result after processing is:
aaa
bbb
aaa
ccc
ddd
ccc
Forum Jump: