Search for a string, then append character to end of that line only

I have 2 files that I am working with

$ cat file1
server1
server3
server5
server6
server8
$ cat file2
server1;Solaris;
server2; SLES;
server3;Linux;
server4; Solaris;
server5;SLES;
server6;SLES;
server7;Solaris;
server8;Linux;

my script

#!/bin/ksh
# if there is a match, write "HIT" at EOL
#set -x

while read LINE; do
    line2=`grep "$LINE" file2`
    if [ -n "$line2" ] ; then
#      awk '/$line2/{print $0, "HIT"}' file2  ##>> file4
#      sed "s/$/ HIT/" file2
#      sed '/$line2/s/$/ HIT/' file2
       sed '/server1/s/$/ HIT/' file2
    else
        echo "$line2" ##>> file4
    fi
done < file1

expected output

server1;Solaris; HIT
server2; SLES;
server3;Linux; HIT
server4; Solaris;
server5;SLES; HIT
server6;SLES; HIT
server7;Solaris;
server8;Linux; HIT

I have been trying both sed and awk but no luck so far.

awk -F";" 'NR==FNR{x[$0]++;next;} {if(x[$1]){print $0" HIT";} else print;}' file1 file2
1 Like