user1200219
user1200219

Reputation: 179

Different between two text file

assuming i have two text file

file 1

    hello i am John
    and i live in Cairo

file 2

    hello i am Jogn 
    and i love in Cairo

and i need to list the different for words only (not space or any thing else) between two text to get the result as file 3 which will contain both words in list like the follwing

    file1     file2
    John      Jogn
    live      love

how i could do that?

i have tried

    diff file1 file2 

but it does not help to get the result as desired

Thanks

Upvotes: 1

Views: 145

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 185540

Using :

awk '
    # BEGIN: print 1th & 2th args
    BEGIN{print ARGV[1], ARGV[2]}
    # if the current line is from "file1",
    # put line in the array "a" with the line number for key
    FNR==NR{a[NR]=$0}
    if current line is from "file2"
    FNR!=NR{
        # iterate over words of the current line
        for (i=1; i<=NF; i++) {
            # split a[key current line] array in array "arr"
            split(a[FNR], arr)
            # test if both file1 and file2 Nth element match
            if (arr[i] != $i) {
                print arr[i], $i
             }
          }
     }
' file1 file2

Output :

/tmp/l1 /tmp/l2
John Jogn
live love

Upvotes: 0

Keith Thompson
Keith Thompson

Reputation: 263507

Use the wdiff command.

If you don't have it, it's in the "wdiff" package, which should be available in your system's repositories.

$ wdiff file1 file2
hello i am [-John-] {+Jogn+} 
and i [-live-] {+love+} in Cairo

If you want a graphical display, the meld program does a pretty good job (install the "meld" package if you don't already have it).

If you need a specific output format, you'll need to write a script. A good start is probably to filter each input file to put each word on a single line (fmt -w 1 is a first approximation) and then diff the results.

Upvotes: 2

Related Questions