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

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 at 15:00
    
Also visit : askubuntu.com/q/12473 –  Pandya Aug 26 at 1:56

5 Answers 5

up vote 10 down vote accepted

Look into the diff command. It's a very powerful 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 (sorry for the lack of cropping):

enter image description here

share|improve this answer
    
Awesome! Thanks a lot –  Roboman1723 Aug 25 at 15:07
2  
@Pandya I feel very wholeheartedly that the screen shot adds to the quality of the answer. –  user973810 Aug 25 at 18:39
    
Can this do other files (such as images)? Or is it limited to just documents? –  Gregory Opera Aug 27 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 at 13:26
    
You can't actually read all about it by doing man diff since GNU refuses to write man pages. –  Samuel Edwin Ward Aug 29 at 17:46

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
    
Looks good, thanks! –  Roboman1723 Aug 25 at 20:43

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
    
Great! +1 for your answer. this is the real answer [output] for this question. –  KasiyA Aug 26 at 7:17

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

Use vimdiff...

Normal syntax :

vimdiff file1 file2

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.