1752805 Members
5729 Online
108789 Solutions
New Discussion юеВ

Compare two files

 
SOLVED
Go to solution
dev44
Regular Advisor

Compare two files

Hi,

I have List A and List B. List A contains a list of excludes. How do I extract from ListB all that is not in List A?
whatever
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor

Re: Compare two files

Hi:

use 'comm' [see the manpages for details].

# comm -23 fileb filea

Regards!

...JRF...
dev44
Regular Advisor

Re: Compare two files

I don't think that will work. I should explain my situation better. File A will contain the following:

/var
/stand
/tmp

Then List B will have something that looks like this:

/abc/def/ghi/jkl/file.ksh
/var/adm/syslog/syslog.log
/tmp/file.txt
/anotherdir/path/file

So I want to output all in FileB except anything with the string in it from FileA. So anything that starts with /var; /stand; or /tmp.

I am trying to find all files of a certain type from all directories BUT the ones in ListA. I have a find that will print out all the files found. Then I was thinking I could somehow grep out all files starting with anything in List A.
whatever
TTr
Honored Contributor
Solution

Re: Compare two files

How about
grep -i -f listA listB

To avoid substring matching probably need to "-w" as well
grep -i -w -f listA listB
TTr
Honored Contributor

Re: Compare two files

I used the wrong option. It should be -v instaead of -i. But it will still miss the substrings.
grep -v -w -f listA listB

James R. Ferguson
Acclaimed Contributor

Re: Compare two files

Hi (again):

> I should explain my situation better.

And I believe I answered what you asked.

http://www.catb.org/~esr/faqs/smart-questions.html

Regards!

...JRF...
TTr
Honored Contributor

Re: Compare two files

The important prerequisite for the "comm" command is that the two files MUST be sorted. Another fact is that the comparison is done on a whole line basis. That is if "/var" is in ListA, then "/var" would be excluded from ListB but "/var/opt" would NOT be excluded from listB.
Apparently in this case that is the behavior you want. In some cases it would not be and the grep command would fail in that case.
Also the grep command would fail in your case as well, if you have "/var" in listA and /usr/opt/common/var/keep" in ListB.
Dennis Handly
Acclaimed Contributor

Re: Compare two files

>So I want to output all in FileB except anything with the string in it from FileA. So anything that starts with /var; /stand; or /tmp.

You will need to change FileA so that you anchor the RE:
^/var
^/tmp
^/stand

Then you can use:
grep -v -f FileA FileB