Shell program in unix

Hi,

Iam having the following situation:

Iam reading one file and taking that line one by one and searching that word in file2 using grep command

read_file1()
{
cat file1| while read line
do
grep $line file2 >> outputfile.dat
done
}

now iam having file 1
q
q
a
s
d

file2
q
q
q
a
s
d

Iam reading file1 and greping that word in file2

Now iam getting duplicates in output file , when iam grepping the same word in file1 multiple times from file2. so how can i avoid this??

Can anybody help??

append "| head -1" to your grep.

Why not remove the duplicates from file1 if that's permitted ?

sort file1 | uniq | while read line

how we can append???

i triyed this
grep $line old_extract.dat append | head -1 > outputfile

This doesn't worked.

fgrep -f file1 file2

No . this frep is also not working

Could you post the output you get and the output you want,
given the data you posted above?

Iam having the following situation:

Iam reading one file and taking that line one by one and searching that word in file2 using grep command

read_file1()
{
cat file1| while read line
do
grep $line file2 >> outputfile.dat
done
}

now iam having file 1
q
q
a
s
d

file2
q
q
q
a
s
d

Iam reading file1 and greping that word in file2

Since the file 1 has duplicates, output file is like

q
q
q
q
q
q
a
s
d

I want to avoid this duplicates in output file.

Can anybody help??

what should be your expected o/p ??????

output should be

q
q
q
a
s
d

u r o/p and the requirement are not matching????
let file1
q
q
a
s
d
and file 2
file2
q
q
q
a
s
d
as per u r requirement 1st line of file 1 came i.e. q and matches with file 2 then goes to o/p file so o/p file has q
then 2nd q came from file 1 and matches with file2 so o/p file have 1 more q
then 3rd line a came and matches with file 2 so o/p have a also
then s , matched => o/p have s
then d came , matched => o/p have d

so u r o/p sud look like
q
q
a
s
d

please correct me if i am wrong and/or not understood u r question in right way ......

Yes you are right.

i should get only 1 q in the output file along with the other data.

sorry for the confusion.

okay, so just run uniq on your final output from your original script.

Remove the word "append".

Anyway, this won't work because you need the output to be uniq. So just pipe the whole script through "uniq". You can do this:

cat file1| while read line ; do
grep $line file2 >> outputfile.dat
done | uniq

But then you might have output like:
q
a
c
q
d
q

If you need to avoid that, you'll need to run it through sort and then uniq.