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
12 Answers
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.
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:
FWIW, I rather like what I get with side-by-side output from diff
diff -y -W 120 File_1.txt File_2.txt
would give something like:
User1 US User1 US
User2 US User2 US
User3 US | User3 NG
You can use the command cmp:
cmp -b "File_1.txt" "File_2.txt"
output would be
a b differ: byte 25, line 3 is 125 U 116 N
Meld is a really great tool. But you can also use diffuse to visually compare two files:
diffuse file1.txt file2.txt
Litteraly sticking to the question (file1, file2, outputfile with "has changed" message) the script below works.
Copy the script into an empty file, save it as compare.py, make it executable, run it by the command:
/path/to/compare.py <file1> <file2> <outputfile>
The script:
#!/usr/bin/env python
import sys
file1 = sys.argv[1]; file2 = sys.argv[2]; outfile = sys.argv[3]
def readfile(file):
with open(file) as compare:
return [item.replace("\n", "").split(" ") for item in compare.readlines()]
data1 = readfile(file1); data2 = readfile(file2)
mismatch = [item[0] for item in data1 if not item in data2]
with open(outfile, "wt") as out:
for line in mismatch:
out.write(line+" has changed"+"\n")
With a few extra lines, you can make it either print to an outputfile, or to the terminal, depending on if the outputfile is defined:
To print to a file:
/path/to/compare.py <file1> <file2> <outputfile>
To print to the terminal window:
/path/to/compare.py <file1> <file2>
The script:
#!/usr/bin/env python
import sys
file1 = sys.argv[1]; file2 = sys.argv[2]
try:
outfile = sys.argv[3]
except IndexError:
outfile = None
def readfile(file):
with open(file) as compare:
return [item.replace("\n", "").split(" ") for item in compare.readlines()]
data1 = readfile(file1); data2 = readfile(file2)
mismatch = [item[0] for item in data1 if not item in data2]
if outfile != None:
with open(outfile, "wt") as out:
for line in mismatch:
out.write(line+" has changed"+"\n")
else:
for line in mismatch:
print line+" has changed"
An easy way is to use colordiff, which behaves like diff but colorizes its output. This is very helpful for reading diffs. Using your example,
$ colordiff -u File_1.txt File_2.txt
--- File_1.txt 2016-12-24 17:59:17.409490554 -0500
+++ File_2.txt 2016-12-24 18:00:06.666719659 -0500
@@ -1,3 +1,3 @@
User1 US
User2 US
-User3 US
+User3 NG
where the u option gives a unified diff. This is how the colorized diff looks like:
Install colordiff by running sudo apt-get install colordiff.
Install git and use
$ git diff filename1 filename2
And you will get output in nice colored format
Git installation
$ apt-get update
$ apt-get install git-core
colcmp.sh
Compares name/value pairs in 2 files in the format name value\n. Writes the name to Output_file if changed. Requires bash v4+ for associative arrays.
Usage
$ ./colcmp.sh File_1.txt File_2.txt
User3 changed from 'US' to 'NG'
no change: User1,User2