中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-12 02:33
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » [Help] How to compare and output the differences between two texts
Printable Version  1,595 / 13
Floor1 junyee Posted 2009-11-25 15:53
中级用户 Posts 112 Credits 253
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.
Floor2 ZJHJ Posted 2009-11-25 21:43
高级用户 Posts 374 Credits 609
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
Floor3 lxmxn Posted 2009-11-26 03:13
版主 Posts 4,938 Credits 11,386
```
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@~ >
```
Floor4 junyee Posted 2009-11-27 18:25
中级用户 Posts 112 Credits 253
Originally posted by lxmxn at 2009-11-26 03:13:

Dude, this isn't a batch script??? Linux???
Floor5 ZJHJ Posted 2009-11-27 21:13
高级用户 Posts 374 Credits 609
The person on the 2nd floor, you don't know how to use it?
Floor6 junyee Posted 2009-11-27 23:21
中级用户 Posts 112 Credits 253
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?>>>???
Floor7 HAT Posted 2009-11-28 01:19
版主 Posts 5,017 Credits 9,023
Before testing, you need to download a grep.exe by yourself.
Floor8 honcho Posted 2009-11-28 12:28
初级用户 Posts 9 Credits 25
@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
Floor9 junyee Posted 2009-11-28 12:41
中级用户 Posts 112 Credits 253
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.
Floor10 ZJHJ Posted 2009-11-28 21:30
高级用户 Posts 374 Credits 609
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 ]
Floor11 honcho Posted 2009-11-29 01:32
初级用户 Posts 9 Credits 25
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)
Floor12 junyee Posted 2010-02-01 22:47
中级用户 Posts 112 Credits 253
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


????
Floor13 ldr2zjj Posted 2010-02-03 20:15
初级用户 Posts 95 Credits 167
Why not use FC? FC is used to compare different ones~~ But I can't write it
Floor14 lovebaby001 Posted 2010-10-01 16:18
新手上路 Posts 12 Credits 12
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
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023