Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

windows - .bat file to compare two text files and output the difference

I am trying something new that I have done on UNIX successfully but have no idea how to do on windows.

So I save a text file, let's say test1.txt and 12 hours later compare the test2.txt (which is test1.txt with changes added during the 12 hours, almost guaranteed to be at the end of the file) to test1.txt and then output just the text differences to a third file, diff.txt

1 action
2 action
3 action
4 action 
5 action

and test2.txt looks like

1 action
2 action
3 action
4 action 
5 action
6 action
7 action
8 action

then the output to the third file diff.txt would look like:

6 action
7 action
8 action

with just the text that has been added, no info regarding lines or comparisons,just a basic output of the differences.

I am COMPLETELY new to this, have looked around and it seems I can write a batch file (.bat) that will basically just act as a UNIX script would.

Sorry for my basic question but I've googled the question and can't seem to figure it out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Simplest and fastest method is using findstr command it will compare and return the result to new file here the script

findstr /vixg:Z:miscest1.txt Z:miscmiscest2.txt > Z:miscmiscest3.txt


findstr /vixg:<source file> <target file> > outputfile

here

/v   : Prints only lines that do not contain a match.
/i   : Specifies that the search is not to be case-sensitive.
/x   : Prints lines that match exactly.
/g: file   : Gets search strings from the specified file.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...