- [Instructor] Hi again, and welcome to
the second video of the third section,
the intersection and set difference, A-B, on text files.
In the previous video, we learned about
generating files of any size.
In this video, we'll learn how to
intersect and set difference on text files.
Intersection and set difference operations
are commonly used in mathematical classes on set theory.
However, similar operations on strings
are also very helpful in some scenarios.
The comm command is a utility to perform
a comparison between the two files.
It has many good options to arrange the output
in such a way that we can perform intersection,
difference, and set difference operations.
Intersection, the intersection operation
will print the lines that the specified files
have in common with one another.
Difference, the difference operation will print the lines
that the specified files contain
and that are not the same in all of those files.
Set difference, the set difference operation
will print the lines in a file, A, that do not match those
in all of the set files specified, B plus C, for example.
Let's see how to do it.
Note that comm takes only sorted files as input.
Take a look at the following example.
First, X-kit comm without any options.
The first column of the output contains
lines that are in A.txt.
The second column contains lines that are only in B.txt.
The third column contains common lines from A and B.txt.
Each of the columns are delimited using
the tab slash-T character.
In order to print the intersection of two files,
we need to remove the first and second columns
and print the third column only as follows.
Print lines that are uncommon in two files as follows.
In this output, columns have their field blank
for each of the unique lines.
Hence, both columns will not have
the content on the same line.
In order to make it more usable,
we need to remove the blank fields
and make two columns into a single column output as follows.
In order to produce a unified output,
use the following command line.
While removing the unnecessary columns,
we can produce the set difference
for A.txt and B.txt as follows.
Set difference for A.txt.
Minus-two, minus-three, remove the second and third columns.
Set difference for B.txt.
Minus-two, minus-three, remove the second and third columns.
The command line options for comm format,
the output has prior requirement.
These are minus-one,
removes the first column from the output.
Minus-two, remove the second column.
Minus-three, remove the third column.
While creating a unified output,
the sed command is piped to the comm output.
The sed removes the slash-T character
at the beginning of these lines.
S in the sed script stands for substitute.
This, slash-carrot, slash-T-slash,
matches the slash-T character at the beginning of the lines.
Carrot is the start of line marker.
No character, slash, slash is the replacement string
for every slash-T character at the beginning of the line.
Hence, every slash-T at the start of a line gets removed.
The set difference operation enables you
to compare two files and print all the lines
that are in the file A.txt or B.txt,
excluding the common lines in A.txt and B.txt.
When A.txt and B.txt are given as arguments
to the comm command, the output will contain column one,
with the set difference for A.txt with regard to B.txt,
and column two will contain the set difference
for B.txt with regard to A.txt.
Awesome.
In this video, we've successfully learned
how to intersect and set difference on text files.
In the next video, we'll see how
to find and delete duplicate files.