Comparing two files
Hi,
I am new to Linux and C++. I want to compare two files and print the values in first file which are not present in second file to the third file. E.g File1 File2 File3 1 1 2 2 3 4 3 5 4 7 5 9 11 As we can see, values from File1 i.e 2 and 4 are not in file2 and have been printed to file3. I know we have to use arrays. Please help me. |
Hi,
1) Is that a homework? 2) If not, is there a reason why you want to do this in C++? 3) Can you show us what you have so far? 4) Are those files sorted? |
No, this is not a homework. This is an officework. I can do the sorting myself. But, I am out of touch with programming and basically want to read from file and put into an array. But if someone can help me with the complete work, I'd be very grateful. It can be in java also not necessarily in C++.
But I'm completely out of touch with java. I can only remember how to compile now! :( |
Well if you're on unix, something like this could do the job (if each file contains each number only once):
Code:
cat "file1" "file2" "file2"|sort -n | uniq -c | awk '{if($1 == 1) print $2}' > file3 Code:
#include <iostream> |
Ehm... read the manual
[edit]
:-[ I've been beaten and overclassed by the previous post. Diff is the tool you want. <code>man diff</code> I thought you could make it show only the lines that were missing from one file, but maybe you need to 'pipe' it through grep. (grep Can get pretty complicated, but it's useful to know you don't need to construct a 'regular expression'. Just giving the string you are looking for as an argument to grep qualifies... in most cases.) Quote:
|
Quote:
Code:
diff file1 file2| awk '{if ($1 == "<") print $2 }' |
Quote:
|
@millgates :
No 11 was in file2. Improper formatting I guess in the code :( I tried diff and the code you put, but it seems to put show all the unique entries from both the files. @Weapon S : Thank you for dedicating your time as well. Thank you to both of you. But I created my own code somehow in haste and I haven't checked the redundancy of LOCs. But it worked for now. :D Code:
|
Quote:
Quote:
Code:
printf("Error opening file ... Press 'Enter' to exit ... "); You also don't have to store both files in memory. One is enough :) |
Yes, I copied the examples correctly. Actually the files I wanted to compare had more than 1000 entries and thats why the whole chaos.
Quote:
One's programming skills can really come handy in life! :D I'll research more with 'diff'. Thanx :) |
Quote:
Code:
$ join -v1 file1 file2 Code:
$ join -v1 <(sort file1) <(sort file2) |
All times are GMT -5. The time now is 08:01 PM. |