changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as

name1:name2:name3:1

when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so on..but if i enter different names than it add those in a fiile with 1 at the end.. any idea will be apreciated

message for mod: I ask this kind of question in one of my other thread but that thread was about something else. So if this is not allowed to ask again even though the topic is different. please delete the post..Thanks

Try this for a starter:

i=1
while grep -qxF "$name1:$name2:$name3:$i" infile
do 
  i=$(( i+1 ))
done
printf "%s\n" "$name1:$name2:$name3:$i" >> infile

Thanks sir, it worked. I was trying this with if else but didnt work out for me. Thanks again

Sorry one problem with this...

What this is doing is
sam:harry:joseph:1 (when i first add the name)

when i enter the same three names it updates that 2 butoutput in the file is like this:
sam:harry:joseph:1 (the one tht we did first)
sam:harry:joseph:2 (number changed)
what i want is it should not skip to next like and change 1 to 2 from the same line which was already stored.

any help will be appreciated
Thanks