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-06-22 06:12
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to compare and output the differences between two texts View 1,511 Replies 13
Original Poster Posted 2009-11-25 15:53 ·  中国 江西 吉安 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Now there are two files a.txt and b.txt, and the contents are assumed as follows:

----a.txt----(This is a comment)
1
2
3
4
5
----end----(This is also a comment, the same below)

----b.txt----
1
b
3
4
5
6
----end----

How to generate through batch processing:

----a-b.txt----(Content that is in a but not in b)
2
----end----

----b-a.txt----(Content that is in b but not in a)
b
----end----

----a~b.txt----(Content that is in both a and b)
1
3
4
5
----end----

Asking all forum friends, is it possible to achieve such an effect with P processing? I know there is a fc command in the system that can compare text, but the generated result is not very intuitive.

Comparison is based on lines, and external commands can be used.
Floor 2 Posted 2009-11-25 21:43 ·  中国 四川 泸州 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
19-year member
UID 59720
Status Offline
cd.>bh.txt
for /f "delims=" %%i in (a.txt) do SET %%i=A
for /f "delims=" %%j in (b.txt) do if /I not defined %%j echo %%j>>bh.txt

-------------------

cd.>bh.txt
for /f "delims=" %%i in (a.txt) do SET %%i=A
for /f "delims=" %%j in (b.txt) do if /I defined %%j echo %%j>>bh.txt
Floor 3 Posted 2009-11-26 03:13 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```
root@~ > grep -xvfb.txt a.txt
2

root@~ > grep -xvfa.txt b.txt
b

root@~ > grep -xfa.txt b.txt
1
3
4
5
6

root@~ >
```
Floor 4 Posted 2009-11-27 18:25 ·  中国 江西 吉安 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Originally posted by lxmxn at 2009-11-26 03:13:
root@~ > grep -xvfb.txt a.txt
2

root@~ > grep -xvfa.txt b.txt
b

root@~ > grep -xfa.txt b.txt
1
3
4
5
6

root@~ >

Dude, this isn't a batch script??? Linux???
Floor 5 Posted 2009-11-27 21:13 ·  中国 四川 泸州 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
19-year member
UID 59720
Status Offline
The person on the 2nd floor, you don't know how to use it?
Floor 6 Posted 2009-11-27 23:21 ·  中国 江西 吉安 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Originally posted by ZJHJ at 2009-11-27 21:13:
You can't use it on the second floor.
,

Thank you for your reply, but may I ask, have you tried it and it worked?>>>???
Floor 7 Posted 2009-11-28 01:19 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Before testing, you need to download a grep.exe by yourself.
Floor 8 Posted 2009-11-28 12:28 ·  中国 北京 东城区 联通
初级用户
Credits 25
Posts 9
Joined 2009-10-17 17:27
16-year member
UID 153217
Gender Male
Status Offline
@echo off
cd.>a-b.txt
cd.>b-a.txt
cd.>a~b.txt
for /f "delims=" %%i in (a.txt) do (
findstr /i "%%i" b.txt > nul || echo %%i>>a-b.txt
)
for /f "delims=" %%i in (b.txt) do (
findstr /i "%%i" a.txt > nul || echo %%i>>b-a.txt
)
for /f "delims=" %%i in (a.txt) do (
findstr /i "%%i" b.txt > nul && echo %%i>>a~b.txt
)
pause
Floor 9 Posted 2009-11-28 12:41 ·  中国 江西 吉安 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Originally posted by honcho at 2009-11-28 12:28:
@echo off
cd.>a-b.txt
cd.>b-a.txt
cd.>a~b.txt
for /f "delims=" %%i in (a.txt) do (
findstr /i "%%i" b.txt > nul || echo %%i>>a-b.txt
)
for /f "deli ...



Thanks. It works well.
Also, thanks to moderator lxmxn for the enthusiastic answer. The method you provided is also feasible.
Floor 10 Posted 2009-11-28 21:30 ·  中国 四川 泸州 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
19-year member
UID 59720
Status Offline
Originally posted by junyee at 2009-11-27 23:21:
,

Thank you for your reply, but, may I ask, have you tried it and it worked?>>>???


You are really too much....... No way.....
setlocal EnableDelayedExpansion
cd.>1.txt
for /f "delims=" %%i in (b.txt) do SET %%i=A
for /f "delims=" %%j in (a.txt) do if /I not defined %%j echo %%j>>1.txt
endlocal EnableDelayedExpansion
cd.>2.txt
for /f "delims=" %%i in (a.txt) do SET %%i=A
for /f "delims=" %%j in (b.txt) do if /I not defined %%j echo %%j>>2.txt
setlocal
cd.>3.txt
for /f "delims=" %%i in (a.txt) do SET %%i=A
for /f "delims=" %%j in (b.txt) do if /I defined %%j echo %%j>>3.txt
Processing your file only takes 0.07 seconds
26.7 times faster than the code in post 8

[ Last edited by ZJHJ on 2009-11-28 at 22:01 ]
Floor 11 Posted 2009-11-29 01:32 ·  中国 江苏 南京 电信
初级用户
Credits 25
Posts 9
Joined 2009-10-17 17:27
16-year member
UID 153217
Gender Male
Status Offline
Originally posted by ZJHJ at 2009-11-28 21:30:


You're really too... no more...
setlocal EnableDelayedExpansion
cd.>1.txt
for /f "delims=" %%i in (b.txt) do SET %%i=A
for /f "delims=" %%j in (a.txt) do if ...



I quite agree, this method is extremely efficient.

Also: In line with the principle of simplicity and optimal efficiency,
the third environment does not need to be changed.
setlocal
cd.>1.txt
for /f "delims=" %%i in (b.txt) do SET %%i=A
for /f "delims=" %%j in (a.txt) do if /I not defined %%j echo %%j>>1.txt
endlocal
cd.>2.txt
cd.>3.txt
for /f "delims=" %%i in (a.txt) do SET %%i=A
for /f "delims=" %%j in (b.txt) do if /I not defined %%j (echo %%j>>2.txt) else (echo %%j>>3.txt)
Floor 12 Posted 2010-02-01 22:47 ·  中国 江西 吉安 遂川县 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Thanks to the诸位 upstairs.

The above three methods are all feasible.

findstr I can understand this, but the execution efficiency seems a bit low. It takes nearly 20 seconds to process a 10K text.

grep This requires a third-party program, and the text cannot be too large. When it reaches a few K, the program will report an error.

10/11Floor This is very magical, but I don't understand it. Can you help explain this usage??.

setlocal
...
endlocal


????
Floor 13 Posted 2010-02-03 20:15 ·  中国 广东 江门 电信
初级用户
★★
Credits 167
Posts 95
Joined 2007-05-06 09:19
19-year member
UID 87628
Gender Male
Status Offline
Why not use FC? FC is used to compare different ones~~ But I can't write it
Floor 14 Posted 2010-10-01 16:18 ·  中国 上海 电信
新手上路
Credits 12
Posts 12
Joined 2010-03-24 20:25
16-year member
UID 162958
Gender Male
Status Offline
I found a simplest code online.

Compare 1.txt and 2.txt, then output the result to text.txt.

Copy the content of the file to be compared into 1.txt.

Copy the content of another file with different content into 2.txt.

Save the following code as a *.bat file.

fc 1.txt 2.txt > text.txt

Then double-click the batch file to generate the text.txt file.

Source from: www.Pay-4u.Com
Complete original article URL: http://www.pay-4u.com/article/18046.html
Forum Jump: