I want to compare two files. I think using grep is the way to go but I do not know how to set this up. Let me show an example:
File1 has 1000 items, File2 has 700 items
I need to take the first item in File1 and search the entire contents of File2 to see if there is a match there. IF there is a match (meaning the same item is in both File1 and File2) then do nothing. IF NOT send that item to an output file named testresults. Something like this:
Open File1,take first variable1
search variable1 in all of File2,
IF variable1 is in File2, do nothing
IF NOT, send variable1 to testresults
Continue loop until all of the items in File1 have been searched for in File2
I thought of using diff and sort, but the problem is that there are much more items in File1 than in File2 which will throw off the results. Another solution might possibly be to use diff and sort in a loop as well? Any help would be appreciated but I believe grep is the easiest way, I could be wrong though
---------- Post updated at 01:30 PM ---------- Previous update was at 01:25 PM ----------
This is what I tried doing, and the weird thing about it is that it seems to work (when I open up testresults, it shows me which files are in file1 that are not in file2), however when i run the code, the command prompt prints out the items that are a match in both files. Why is it doing this?
for i in `cat file1`
do
grep $i file2
if [ ! $? -eq 0 ]
then
{
echo $i >> testresults
}
fi
done
Also tried this (someone suggested using this) but results came out blank... not sure why...
(
awk -F"/n" '
BEGIN {
while( getline < "'file2'" )
{ arr[file1]=1 }
}
arr[file1] != 1 { print }
' file1
) > testresults2