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

LinuxGrep

Linux Problem Overview


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".

Linux Solutions


Solution 1 - Linux

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 *

Solution 2 - Linux

grep -F -r '0.49' * treats 0.49 as a "fixed" string instead of a regular expression. This makes . lose its special meaning.

Solution 3 - Linux

You need to escape the . as "0\.49".

A . is a regex meta-character to match any character(except newline). To match a literal period, you need to escape it.

Solution 4 - Linux

There are so many answers here suggesting to escape the dot with \. but I have been running into this issue over and over again: \. gives me the same result as .

However, these two expressions work for me:

$ grep -r 0\\.49 *

And:

$ grep -r 0[.]49 *

I'm using a "normal" bash shell on Ubuntu and Archlinux.

Edit, or, according to comments:

$ grep -r '0\.49' *

Note, the single-quotes doing the difference here.

Solution 5 - Linux

You can also use "[.]"

grep -r "0[.]49"

Solution 6 - Linux

Just escape the .

grep -r "0\.49"

Solution 7 - Linux

Escape dot. Sample command will be.

grep '0\.00'

Solution 8 - Linux

You can escape the dot and other special characters using \

eg. grep -r "0\.49"

Solution 9 - Linux

You can also search with -- option which basically ignores all the special characters and it won't be interpreted by grep.

$ cat foo |grep -- "0\.49"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVarun kumarView Question on Stackoverflow
Solution 1 - LinuxgeekosaurView Answer on Stackoverflow
Solution 2 - LinuxJoniView Answer on Stackoverflow
Solution 3 - LinuxcodaddictView Answer on Stackoverflow
Solution 4 - Linuxq9fView Answer on Stackoverflow
Solution 5 - LinuxskanView Answer on Stackoverflow
Solution 6 - LinuxcppcoderView Answer on Stackoverflow
Solution 7 - LinuxtuxudayView Answer on Stackoverflow
Solution 8 - LinuxKevin DavisView Answer on Stackoverflow
Solution 9 - LinuxsmcView Answer on Stackoverflow