If with sed loop question

Hi,

$list_A contains a list of user ID's.
$pwdfile_new is the unix password file.
$useralias is a report file

I'm trying to search through the password file with the list in list_A and dumping what ever name it matches into the useralias file. But I want to the code to put "Unable to resolve user" into the report if it cannot find the ID in the password file.. I was trying the below but its not working..

Can anyone suggest where I'm going wrong

        
for i in $(cat $list_A)
        do
                to_replace=$(grep -w $i ${pwdfile_new} | cut -f2 -d"=")
                matchuser=$(grep -wc $i ${pwdfile_new})

                echo $matchuser $i

                if [[ "${matchuser}" != "1" ]]
                        then
                        sed "s/$i/$i =Unable to resolve user}/g" ${useralias} > $file_A
                        mv $file_A ${useralias}
                else
                        sed "s/${i}/${to_replace}/g" ${useralias} > $file_A
                        mv $file_A ${useralias}
                fi
done

EDIT***

Found the issue... Eventually...

Useless Use of Cat

Useless Use of grep | awk

Also, this loop looks very inefficient.

If you show the input you have and the output you want, there's probably much better ways to do it.

I'll admit it aint the prettiest piece of code ever but it half works and I'm under pressure to get this script finished for the business..

I've 4 files I'm working with here..

${grouplist}_temp2 Contains a list of the groups in the report. E.g.
OCC
root
admin
printgroup

$list_C contains a list of all the secondary user ID's belonging to the Groups in the ${grouplist}_temp2 E.g.
x111222333
x111333444
x224422333

$pwdfile_new contains user ID's and the names associated with them

${grouplist}_2 is a file which contains the group names and all the xid's asociated with them. E.g.
OCC=x1332223, x4324523, w2324233, x12451213
admin=x2136423, x1332223, w2324233, x4444444, x1212323
printgroup=x12451213

I need to replace the group ID's in the ${grouplist}_2 file with their matched username E.g. x111222333=Joe Bloggs
But I also need it to return Name_Not_Found if the xid is not contained in that $pwdfile_new file..
E.g. x224422333=Name_Not_Found

       for y in $(cat $list_C)
        do
                to_replace9=$(grep -w ${y} ${pwdfile_new} | cut -f2 -d"=")
                matchgrpusr=$(grep -wc ${y} ${pwdfile_new})
                if [[ "${matchgrpusr}" != "1" ]]
                then    
                    sed "s/$y/${y}=Name_Not_Found/g" ${grouplist}_2 > $file_B
                    mv $file_B ${grouplist}_2
                else    
                #### Making sure to replace the second instance of $y if the xid is the same name as the user
                matchfirstinst=$(grep -wc $y ${grouplist}_temp2)
                if [[ "${matchfirstinst}" == "1" ]]
                        then
                        sed "s/$y/${y}=${to_replace9} /2" ${grouplist}_2 > $file_B
                        mv $file_B ${grouplist}_2
                else
                sed "s/$y/${y}=${to_replace9} /g" ${grouplist}_2 > $file_B
                mv $file_B ${grouplist}_2
                fi
                fi
        done

If someone can suggest a better way that would be much appreciated...

EDIT****

Ok I've changed it around a bit but when it finds a match it should be echoing out $useridvar $to_replace9 but it seems to be echoing out echo $useridvar $matchgrpusr instead... Been looking at this so long now nothings making sense

while read useridvar
do
#to_replace9=$(grep -w ${useridvar} ${pwdfile_new} | cut -f2 -d"=")
matchgrpusr=$(grep -wc ${useridvar} ${pwdfile_new})
    if [[  "{$matchgrpusr}" == 1 ]]
                then    
                to_replace9=$(grep -w ${useridvar} ${pwdfile_new} | cut -f2 -d"=")
                echo $useridvar $to_replace9
                sed "s/${useridvar}/${useridvar}=${to_replace9} /g" ${grouplist}_2 > $file_B
                mv $file_B ${grouplist}_2

                else
                echo $useridvar $matchgrpusr
                sed "s/$useridvar/${useridvar}=Name_Not_Found/g" ${grouplist}_2 > $file_B
                mv $file_B ${grouplist}_2
     fi
done < $list_C