Link to home
Start Free TrialLog in
Avatar of damijim
damijimFlag for United States of America

asked on

Comparing two text files?

Hello,
I've been playing around with 2 files for a while now; I'm trying to find the difference in the two plain text files. I only want to output the non-matching lines & ignore white space)

I have access to Windows, Linux, or IBM's AIX [Unix] available.

I've played with, diff, comm, grep, etc, but have not gotten very far yet.

My main objective is to find out what files are missing comparing two directory structures. I'm not concerned about the file size, just the file's name.

Thanks!
Avatar of farzanj
farzanj
Flag of Canada image

Use diff -b to disregard differences in white spaces.

If you want to compare two directory structures, you can prepare a sorted list of the file names.

cd DIR1
ls | sort > directory1

cd DIR2
ls | sort > directory2

Now

diff directory1 directory2

or
comm directory1 directory2

This should work now because you have a sorted list of files
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A few comments on farzanj's post:

> ls | sort > directory1
I think the "sort" is not required, since "ls" automatically sorts filenames.

> cd DIR1
There's no need to "cd" to the directories.  You can do:
    ls DIR1 >directory1
    ls DIR2 >directory2
    diff directory{1,2}   # Or the full syntax
And "ls" won't show the directory1/2 names its output.
Avatar of damijim

ASKER

I do not have a GUI for this AIX box.

fraznaj,
I have tried that and I end up with a mess of code. i know there are only a couple missing lines between the plain text. I've tried your suggestion already and it didn't work out.
you said "I have access to Windows"

Beyond Compare is a windows program - so I don't quite understand why it won't work.
You should use

ls -a -1

to get one-column output and to show hidden files as well. Please note that it's the digit "1", not the letter "l"!

Next, are you aware that comm provides a three-column output?

1. Lines that are only in File1
2. Lines that are only in File2
3. Lines that are in both File1 and File2.

You can suppress displaying single columns by specifying the appropriate column numbers as a flags to comm, e.g.

comm -2 -3 file1 file2

will only display column 1.

So try

ls -a1 dir1 > dir1.out
ls -a1 dir2 > dir2.out

diff -w dir1.out dir2.out
or
comm -3 dir1.out dir2.out

"-w" of "diff" ignores all spaces and tab characters and treats all other strings of blanks as equivalent. Unfortunately there is no such option with "comm".

wmp
Avatar of damijim

ASKER

Sorry for the confusion. The first resolution that worked out for me was Beyond Compare. It made it really simple to find the data I needed. Thanks.