Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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
share|improve this question
5  
Use diff "File_1.txt" "File_2.txt" –  Pandya Aug 25 '14 at 15:00
    
Also visit : askubuntu.com/q/12473 –  Pandya Aug 26 '14 at 1:56

8 Answers 8

up vote 17 down vote accepted

Look into the diff command. It's a good tool, and you can read all about it by typing in your terminal man diff.

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:

enter image description here

Additionally you might notice the 4th command is diff ... | tee Output_File this outputs the results from tee 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.

share|improve this answer
    
Can this do other files (such as images)? Or is it limited to just documents? –  Gregory Opera Aug 27 '14 at 13:10
    
As far as I know, it's limited to text files. Code will work, as it's essentially text, but any binary files (which pictures are) will just get junk out. You CAN compare to see if they're identical by doing: diff file1 file2 -s. Here's an example: imgur.com/ShrQx9x –  Musher Aug 27 '14 at 13:26

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:

enter image description here

Compare directory:

enter image description here

Example with full of text:

enter image description here

share|improve this answer

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"
share|improve this answer

Meld is a really great tool. But you can also use diffuse to visually compare two files:

diffuse file1.txt file2.txt

enter image description here

share|improve this answer

In addition there is comm command, which compares two sorted files, and gives output in 3 colums : column 1 for items unique to file #1, column 2 for items unique to file #2, and column 3 for items present in both files.

To suppress either column you can use switches -1, -2 , and -3. Using -3 will show the lines that differ.

Bellow you can see the screenshot of the command in action.

enter image description here

There is just one requirement - the files must be sorted for them to be compared properly. sort command can be used for that purpose. Bellow is another screenshot , where files are sorted and then compared. Lines starting on the left bellong to File_1 only , lines starting on column 2 belong to File_2 only

enter image description here

share|improve this answer
    
@DavidFoerster it's kinda hard to be editing on mobile :) Done now, though –  Serg May 23 at 9:17
    
Thanks and +1. :-) –  David Foerster May 23 at 10:58

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
share|improve this answer

You can use vimdiff.

Example:

vimdiff  file1  file2
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.