How to compare two files
So basically what I want to do is compare two file by line by column 2. How could I accomplish this?
File_1.txt:
User1 US
User2 US
User3 US
File_2.txt:
User1 US
User2 US
User3 NG
Output_File:
User3 has changed
Solution 1:
Look into the diff
command. It's a good tool, and you can read all about it by typing man diff
into your terminal.
The command you'll want to do is diff File_1.txt File_2.txt
which will output the difference between the two and should look something like this:
A quick note on reading the output from the third command: The 'arrows' (<
and >
) refer to what the value of the line is in the left file (<
) vs the right file (>
), with the left file being the one you entered first on the command line, in this case File_1.txt
Additionally you might notice the 4th command is diff ... | tee Output_File
this pipes the results from diff
into a tee
, which then puts that output into a file, so that you can save it for later if you don't want to view it all on the console right that second.
Solution 2:
Or you can use Meld Diff
Meld helps you compare files, directories, and version controlled projects. It provides two- and three-way comparison of both files and directories, and has support for many popular version control systems.
Install by running:
sudo apt-get install meld
Your example:
Compare directory:
Example with full of text:
Solution 3:
You can use vimdiff.
Example:
vimdiff file1 file2