I am having a variable like a='Hope'. And I am having a file containing sentences like Time delay go straight Hope
In this file I need to find "Hope" only. And I need to compare that with the variable "a". How it will possible
I am having a variable like a='Hope'. And I am having a file containing sentences like Time delay go straight Hope
In this file I need to find "Hope" only. And I need to compare that with the variable "a". How it will possible
if grep -q -wF "$a" file.txt; then
printf 'The file contains the word "%s"\n' "$a"
else
printf 'Did not find "%s" in this file\n' "$a"
fi
grep -wF
will look for a particular word in the given file (file.txt
in this case). The -F
option tells grep
that the pattern, $a
, is a fixed string, not a regular expression. The -w
option will make sure that you don't get false positives from words like Hopeless
if $a
is Hope
.
The -q
option tells grep
not to produce any output. Instead we use the exit status of grep
to see whether there was a match in the file or not.