Searching for exact match using grep

I am searching for an exact match on a value read from another file to lookup an email address in another file. The file being checked is called "contacts" and it has Act #, email address, and contact person.

1693;abc1693@yahoo.comt;Tommy D
6423;abc6423@yahoo.comt;Jim Doran
6491;abc6491@gamil.comdt;Snowden
155;xyz155@gmail.com; Lenny Melfi
442;xyz422@test.com; Tommy Davis
2155;abc2155@google.com; Cindy F
email=`grep $acc /u/star/contacts|cut -f2 -d';'`

The results give me 155 and 2155 and I only want to get 155 as a match so I get email = xyz155@gamail.com and email = abx2155@google.com. Any suggestions on how to get only 155?

You may try awk

$ awk -F";" '$1==search{print $2}' search="155" file
xyz155@gmail.com
akshay@Aix:/tmp$ cat a
1693;abc1693@yahoo.comt;Tommy D
6423;abc6423@yahoo.comt;Jim Doran
6491;abc6491@gamil.comdt;Snowden
155;xyz155@gmail.com; Lenny Melfi
442;xyz422@test.com; Tommy Davis
2155;abc2155@google.com; Cindy F
$ awk -F";" '$1==search{print $2}' search="155" file
xyz155@gmail.com

for variable

$ acc=155
$ email=$(awk -F";" '$1==search{print $2}' search="$acc" file)
$ echo $email
xyz155@gmail.com
$ grep "^155" file | cut -d";" -f2
xyz155@gmail.com
1 Like
grep "^$acc;" "$file"
1 Like