Using Grep to Search for a String That Has a Dot in It

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

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 *

dot in grep command being used as regex

Looks like I have immediately found the answer after RTFMing a little closer

-G, --basic-regexp
Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.

"This is the default" - I assume means this is the default behaviour if no flags are passed?

Search(with grep) in file with variable that contains dot

From the grep man page:

-w, --word-regexp

Select only those lines containing matches that form whole words.

$ grep -Fw "$var" myFile.txt | awk '{ print $2 }'
A

Also, even if we turn off globbing set -f before running the grep, we still need to explicitly specify the -F switch for grep to not to treat the string as regex.

How to grep for the exact word if the string has got dot in it

You need to escape the . (period) since by default it matches against any character, and specify -w to match a specific word e.g.

grep -w -l "BML\.I" *

Note there are two levels of escaping in the above. The quotes ensure that the shell passes BML\.I to grep. The \ then escapes the period for grep. If you omit the quotes, then the shell interprets the \ as an escape for the period (and would simply pass the unescaped period to grep)

difficulties with finding the names that have dot in them with grep

You need to escape those dots. Because grepl (without fixed=TRUE argument) should accept regex as first argument. Dot's in regex matches any character. IN-order to match literal dot's, you need to escape them in the regex part. Or you may use char class for treating those as literal ones like [.]

grepl("J\\.D\\. Drew" ,data_player$name)

Grepping for exact string while ignoring regex for dot character

fgrep is the same as grep -F. grep also has the -x option which matches against whole lines only. You can combine these to get what you want:

grep -Fx account.test file.txt

Remove extra dot in a String by grep/awk/cut

You may use this grep:

grep -oE -m1 'test/[^[:blank:]]+\.[^.]+' file

test/20210804144418.zip

RegEx Details:

  • test/: Match test/
  • [^[:blank:]]+: Match 1+ of non-whitespace characters
  • \.: Match a dot
  • [^.]+: Match 1+ of any non-dot characters


Related Topics



Leave a reply



Submit