Using grep to search for a string that has a dot in it

Varun kumar picture Varun kumar · Apr 27, 2012 · Viewed 153.2k times · Source

I am trying to search for a string 0.49 (with dot) using the command

grep -r "0.49" *

But what happening is that I am also getting unwanted results which contains the string such as 0449, 0949 etc,. The thing is linux considering dot(.) as any character and bringing out all the results. But I want to get the result only for "0.49".

Answer

geekosaur picture geekosaur · Apr 27, 2012

grep uses regexes; . means "any character" in a regex. If you want a literal string, use grep -F, fgrep, or escape the . to \..

Don't forget to wrap your string in double quotes. Or else you should use \\.

So, your command would need to be:

grep -r "0\.49" *

or

grep -r 0\\.49 *

or

grep -Fr 0.49 *