How to compare 2 files.

Hi,

I want to compare 2 files as below

file1:
--------
yellow:sure
blue:567
red:75843kjsad

file2:
--------
yellow:sujji
blue:summi
orange:girri
red:akash
white:alias

Output:
---------
yellow:sujji
blue:summi
red:akash

so please let me know how i can achive the same using script

What have you tried so far and where are you stuck?

Regards

What are you trying to accomplish? From the way that this is presented, it looks like homework - and that is not allowed per the Forum Rules.
The UNIX and Linux Forums - Forum Rules

iam new to shell script, & i tried the below code.
#!/bin/bash
#To compare the password file
echo "File comparision"
awk -F';' 'BEGIN{while(getline<"file1") a[$1]=2};a[$1]!=2' file2> listdifference

cksum file1 | awk '{print$1}' > x
cksum file2 | awk '{print$1}' > y
x=`cat x`
y=`cat y`

function test
{
if [ $x -le $y ]
then
echo "First file x is smaller. Add difference into smaller file x and remove the difference."

paste -d"\n" listdifference file1 | sort | uniq > lists
awk NF lists > listfinal

while read difference
do
echo "Deleting line $difference"
#sed "/$difference/d" listfinal > output
sed -e "s/$difference//g" listfinal > output
done < listdifference
else
echo "Second file y is bigger. Add line into x."
fi
}
test

--------------
after running the above script i got the below list to the listdifference file.
yellow:sujji
blue:summi
orange:girri
red:akash
white:alias

but i need is if any changes are made in the file2 that has the entries in file1 i need to replace that entry in file1 with the value of file2.

ex: in file1 we have an entry called 'yellow:sure' if i changed the value of the same in file2 to 'yellow:somethingelse' then it has to replace with the same in file1 for the value of 'yellow:sure' .

> cat file41
yellow:sure
blue:567
red:75843kjsad
> cat file42
yellow:sujji
blue:summi
orange:girri
red:akash
white:alias

> cat diff_40
#! /usr/bin/bash

cut -d":" -f1 <file41 >file41a
cut -d":" -f1 <file42 >file42a

comm -12 file41a file42a >file4_comm

egrep -f file4_comm file42 >file42f

cat file42f

Program execution; and display of results

> diff_40
yellow:sujji
blue:summi
red:akash
> 

Have a look at the following commands:
diff
sdiff

(and for binary files cmp)
Good luck

Thank you, the code provided by you people are working fine if the lines in both the files are in same order. but if the lines are not in same arder it fails.