Solved

Comparing two text files?

Posted on 2011-03-21
7
708 Views
Last Modified: 2013-11-17
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!
0
Comment
Question by:damijim
7 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 35186081
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
0
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 500 total points
ID: 35186123
If you can use a GUI, then try Beyond Compare.  Otherwise, you can use Notepad++ - it doesn't show "JUST" the differences, but it highlights them.
0
 
LVL 11

Expert Comment

by:tel2
ID: 35186319
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.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:damijim
ID: 35186338
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.
0
 
LVL 95

Expert Comment

by:Lee W, MVP
ID: 35186376
you said "I have access to Windows"

Beyond Compare is a windows program - so I don't quite understand why it won't work.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 35187714
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
0
 
LVL 1

Author Closing Comment

by:damijim
ID: 35191572
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now