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/
: Matchtest/
[^[:blank:]]+
: Match 1+ of non-whitespace characters\.
: Match a dot[^.]+
: Match 1+ of any non-dot characters
Related Topics
Linux Kernel System Call Returns -1 Instead of {-1, -256}
Can't Su to User Jenkins After Installing Jenkins
Using Grep to Search for a String That Has a Dot in It
One Command to Create a Directory and File Inside It Linux Command
Which Drivers Are Used by Usb Mouse in Linux Kernel
Minimal Core Dump (Stack Trace + Current Frame Only)
Why Does Arm-Linux-Gnueabi-G++-4.4 Always Build a "7-A" Binary
Combine a Shell Script and a Zip File into a Single Executable for Deployment
How to Exclude Absolute Paths for Tar
What Sort Order Does Linux Use
How to List (Ls) the 5 Last Modified Files in a Directory
How to Start a Shell Without Any User Configuration
Grep from Tar.Gz Without Extracting [Faster One]
Detecting Keyboard, Mouse Activity in Linux
Bash: How to Tokenize a String Variable
How to Get Exit Status of a Shell Command Used in Gnu Makefile