Get the exact match of the string!

Hi All,

I am breaking my head in trying to get a command that will exactly match my given string. I have searched net and found few of the options -

 
grep -F $string file
grep -x $string file
grep "^${string}$" file
awk '/"${string}"/ {print $0}' file

strangely nothing seems to work in my script?! I'm not sure what the problem is, I'm calling this command in a while loop

 
while read string
do 
 <above commands>
done < file

and the $string is not getting expanded in some cases while in some cases it attaches ^$ to the string and nothing "grep":confused:. Please tell me what I'm doing wrong.

-dips

Can you post sample data and value assigned to variable "string"

Your awk won't work as a) the variable is not expanded within single quotes b) the /.../ needs a string constant not a variable (while when expanded between double quotes there's a chance to work).

grep -F $string file                 # The variable needs to be put in double quotes 
grep -x $string file                 # This is a whole line match, but $string is interpreted as a regex 
                                     # and the variable needs to be put in double quotes 
grep "^${string}$" file              # This is a whole line match, but $string is interpreted as a regex
awk '/"${string}"/ {print $0}' file  # This will not work, see RudiC's comments

Also make sure there are no CR characters in your file (dos format) or none of the exact line matching will work...