Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

windows

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.

like image 628
Martin O Leary Avatar asked Oct 24 '12 08:10

Martin O Leary


People also ask

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

What does && do in batch file?

&& runs the second command on the line when the first command comes back successfully (i.e. errorlevel == 0 ). The opposite of && is || , which runs the second command when the first command is unsuccessful (i.e. errorlevel != 0 ). @Spandy: web.archive.org/web/20060412075633/https://www.microsoft.com/…

Which is better CMD or BAT?

CMD is faster and more stable when compared with BAT. The execution is both the scripts are different due to the error variable. The error variable is always reset to zero in CMD. The error variable remains the same unless and error is shown in BAT scripts.


1 Answers

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:\misc\test1.txt Z:\misc\misc\test2.txt > Z:\misc\misc\test3.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.
like image 139
jacob justin Avatar answered Sep 20 '22 16:09

jacob justin