In this video, we're going to show you a really useful Unix tool called diff,
which takes two files and compares them and tells you differences in them.
You're going to use this in an upcoming assignment and in
many other assignments to compare the output your program generates,
with the output that we've provided for you so that you can see if your output is right.
So in this upcoming program assignment,
you're going to write a program which prints out some squares,
and takes some command line arguments,
and prints out a pattern of square.
So if I do ls, I'm going to see that I have some files here called like ans_3_5_8_2.txt,
which has this little pattern of squares.
And I've already done my assignment and compiled it into this program squares.
So if I run squares with 3_5_8_2,
I want the same thing, so I can look at these,
they look pretty much right.
Now, for big complicated things,
might be really tedious,
or complicated, or hard to look at them and see if they're the same.
So what I'd like to do is redirect the output of my program to a file,
so rather than printing it to a screen, it writes it to a file.
You should have seen that in your readings about Unix,
and I'm going to call it like, myout.txt.
So we're to cat that file, I now see what's there,
but if I were to run diff myout.txt with ans_3_5_8_2, diff says nothing.
So that's good. That means that we have exactly the same files,
my output matches the expected output, everything is good.
But I've also made a version of this that's messed up, called squares_broken.
So if I run that,
so now it looks different,
then if I diff myout now with ans_3_5_8_2,
it's going to tell me that these are different.
Now this output format from diff may seem a little weird,
what this means with less than sign as these are lines that were in the first file,
that is this argument to diff,
but not the second one.
And these were things that diff found in the second file, this one.
So it's pointing that way and not that one.
We can get a slightly more readable version
of this if I give it dash y to give me side by side output.
So it will show me that this and this are lined up,
and this little line down the middle means that these are different.
And you might say but true like this looks like a blank line,
and this looks like a blank line.
They actually have different amounts of white space on them.
We can tell diff to ignore white space and things like that.
If we read man diff,
remember man tells us all the useful things about any command or library function,
and we look for white space.
There we go, it's two words.
We can see that there's a bunch of options for
ignoring white space including dash W and we'll ignore all white space.
So if I didn't want it to treat differences and spacing differently,
I can put dash W, that's a little dangerous because now it thinks that,
this is the same as this,
because they only differ by a space,
thinks this line is in one file,
this line is the same as this one,
these lines it thinks are the same line but different,
meaning it thinks the lines in both files but changed.
And it thinks this line appears only in this file.
So sometimes it interprets things a little bit weirdly,
but it will do a good job of that.
We can use different white space options if we wanted,
based on what you need,
so we might want to ignore only trailing white space for example, or something like that.
But in this case, we can look at these side by side,
see what it thinks is different and figure out whether we've got the right output or not.