1826415 Members
3853 Online
109692 Solutions
New Discussion

Re: comparing 2 files

 
SOLVED
Go to solution
Marcia Ferreira
Advisor

comparing 2 files

Hi people

I have 2 files: t1 and t2 - both of them has a list of disk devices. I intend to compare both and extract de difference. The problem is diff and comm, compare lines, and I want to compare all the differences.
Does somebody know how can I compare those files?

regards
Marcia Ferreira
Marcia Ferreira
27 REPLIES 27
Jean-Louis Phelix
Honored Contributor

Re: comparing 2 files

Hi,

Asumming that files are sorted before, both tools could do it. Else you can give us some line of both files to check format.

Regards.
It works for me (© Bill McNAMARA ...)
curt larson_1
Honored Contributor

Re: comparing 2 files

have you tried using sdiff?
Michael Schulte zur Sur
Honored Contributor

Re: comparing 2 files

Hi Marcia,

what do you mean by all the differences? Sort the two file as mentioned before and diff should work. Can you explain a bit more, what differences you mean?

greetings,

Michael
Mark Greene_1
Honored Contributor

Re: comparing 2 files

sdiff -s -w [len] file1 file 2 |pg

where [len] is the length of the longest line in either file +1. The -s option tells sdiff to not display lines that are exactly alike. Differeneces in whitepspace (spaces or tabs) are counted as a valid difference.

mark
the future will be a lot like now, only later
Sathish C
Frequent Advisor

Re: comparing 2 files

Hi
Considering that the file t1 and t2 has got the device names sorted by unique key ( sort -u ) and you can also use with -c option to see there are no lines with duplicate keys in addition to checking that input file is sorted and the to a diff netween them will suffice your request .

Sathish C
Some cause happiness wherever they go; others, whenever they go
Marcia Ferreira
Advisor

Re: comparing 2 files

Hi guys

the problem is t1 has 26 lines, and has disks device. t1 has 27 lines and has disk device too. There are some common devices; I want to create a new file e.g t3, that will have only the different disk devices. When I use diff, comm or sdiff, they compare line to line, not line to file.

regards
Marcia
Marcia Ferreira
Sathish C
Frequent Advisor

Re: comparing 2 files

hi
that was exactly what almost everyone sujjested to you , sort the file

sort -u t1 > t1.s
sort -u t2 > t2.s
diff t1.s t2.s

should give you the result .
Some cause happiness wherever they go; others, whenever they go
Jean-Louis Phelix
Honored Contributor

Re: comparing 2 files

Hi,

I'm very sorry, but comm *can* do it if files are sorted first, or you forgot to tell us something about your files. Give us some lines of both or try sort then comm.

Regards.
It works for me (© Bill McNAMARA ...)
Elmar P. Kolkman
Honored Contributor

Re: comparing 2 files

If that is what you want, it can be done like this:

prev=
cat t1 t2 | sort -u | while read line
do
if [ `grep -q "$line" t1 t2 | wc -l` -eq 1 ]
echo $line >> t3
fi
done

Or with awk or perl, using the sorted output of both files. But this is the simplest solution I can think of right now.
Every problem has at least one solution. Only some solutions are harder to find.
Elmar P. Kolkman
Honored Contributor

Re: comparing 2 files

Problem with diff is that it will give more output then just the lines that are not in both files...

(the prev= line in my solution is nonsense. Was a try that I ignored)
Every problem has at least one solution. Only some solutions are harder to find.
Marcia Ferreira
Advisor

Re: comparing 2 files

Hi Jean

I did sort -u first. I will send all the files to you understand what I want.
The scene is: I hae to create 2 VGs with sequencial disk devices, but I have others VG created on the server. I have to compare all the disk devices captured by ioscan, and compare with the PVs related to the VGs created. The free disk devices will use to create a new VG. (t1 is the list of a VG had created and t2 is the list of ioscan)

tks

Marcia
Marcia Ferreira
john korterman
Honored Contributor

Re: comparing 2 files

Hi,
if you have two files like this:

# cat file1
fup1
fup2
fup3
fup4
fup5

# cat file2
fup1
fup2
fup4
fup5
fup6


# comm -3 file1 file2
fup3
fup6

Is that not what you want?

regards,
John K.

it would be nice if you always got a second chance
Mark Grant
Honored Contributor

Re: comparing 2 files

Well, I am convinced that this is an overly complex way to do it but it is the first thing that came into my head and it does actually work.

#!/usr/bin/perl

open FILE1, "open FILE2, "
while($i=){
chomp($i);
$LIST{$i}++;

}

while($i=){
chomp($i);
$LIST{$i}++
}

foreach $i (keys %LIST){
if($LIST{$i} == 1){
print "$i\n";
}
}

I am sure there are a lot quicker and more sensible ways this can be done
Never preceed any demonstration with anything more predictive than "watch this"
Mark Grant
Honored Contributor

Re: comparing 2 files

Wow Jon!

I had never even heard of the "comm" command. Looks good to me though :)
Never preceed any demonstration with anything more predictive than "watch this"
Marcia Ferreira
Advisor

Re: comparing 2 files

Hi John

your sugestion didn´t work. If you see the attachment that I sent, you will understand why.

tks
Marc
Marcia Ferreira
Elmar P. Kolkman
Honored Contributor

Re: comparing 2 files

I think you need to re-create one of the files: one uses the block devices, while the other one uses the raw devices !!!

Or you could use cut to get only the device name and ignore the first 2 parts of the device path:
cut -d/ -f4

Every problem has at least one solution. Only some solutions are harder to find.
john korterman
Honored Contributor

Re: comparing 2 files

Hi again,
actually, I did not see your attachment before I posted. Therefore I did not understand that the issue was comparing disk devices with raw devices.

regards,
John K.
it would be nice if you always got a second chance
john korterman
Honored Contributor
Solution

Re: comparing 2 files

Hi again,
how about:
# cat t1| awk -F/ '{print $4} > t1.lim
# cat t2| awk -F/ '{print $4} > t2.lim

The way it was in the detachment caused it to be $4, the idea is to have only the last parts of each line in the files. Then
# comm -3 t1.lim t2.lim
c19t0d0
c19t13d6
c19t13d7
c19t14d0
c19t14d1
c19t14d6
c19t14d7
c19t2d0
c19t3d0
c19t4d0
c19t8d0
c19t8d1
c19t8d2
c19t8d3
c21t12d0
c21t12d1
c21t12d2
c21t12d3
c21t12d4
c21t12d5
c21t12d6
c21t12d7
c21t13d0
c21t13d1
c21t13d2
c21t13d3
c21t13d4

will that do for at starting point?
regards,
John K.



it would be nice if you always got a second chance
Marcia Ferreira
Advisor

Re: comparing 2 files

Hi John

sorry, but I sent the wrong file. My partner save another file with the same name.
Your solution was right. The problem finished.

thanks a lot

Marcia
Marcia Ferreira
john korterman
Honored Contributor

Re: comparing 2 files

sorry, my last posting does of course not show the indents. I could at least have put a separator between
c19t8d3

and

c21t12d0

but I forgot. And I am aware that the posting looks idiotic, but try to imagine that everything before before c21t12d0 is indented and the rest is not. Believe me, it looks much better on my screen!

regards,
John K.
it would be nice if you always got a second chance
Michael Schulte zur Sur
Honored Contributor

Re: comparing 2 files

I wonder, if I got it right?

cat t1 | xargs -n1 -I'{}' basename '{}' > t1.cut
cat t2 | xargs -n1 -I'{}' basename '{}' > t2.cut
diff t1 t2

greetings,

Michael

Marcia Ferreira
Advisor

Re: comparing 2 files

No problem John ... no problems at all ...

best regards

Marcia Ferreira
Marcia Ferreira
Alex Ostapenko
Advisor

Re: comparing 2 files

Hi Marcia!

I know you said that you solved your problem, but I thought I might offer another way in case you run into this in the future...

If I understand your problem correctly:

t1 = set of disks already in use by VG's
t2 = set of all disks on system
t3 = set of available disks = t2-t1

...right?

Here is a way (not mentioned yet) that one could solve the problem:

1. convert t1 to just the base device name
cat t1 | awk -F/ '{print $4}' | sort -u >t1.modified

2. use reverse sense "grep" to exclude from t2 what is already in t1.modified
cat t2 | grep -v -f t1.modified >t3

I tested this on your input files, and it seems to work. Good luck!

=:-) Alex
Marcia Ferreira
Advisor

Re: comparing 2 files

Hi Alex

OK ... your suggestion is good too. I´ll use it in another script.

tks

Marcia Ferrei
Marcia Ferreira