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
)
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
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 *
Grep for exact match when there is no more words
Use this expression
grep -i ^word$ file
Explanation:
Find all lines starting with (^) at the end of the line ($).
The -i flag makes the match insensitive, remove it if you want a case sensitive match
grep not consider dot in exact search
You can use a regex:
cat tlds.csv | grep -E ',com\.csv$'
In this way, you tell grep that you want only those lines that have a ,
before com.csv
, and nothing more after com.csv
.
You can also use
cat tlds.csv | grep -E '[^.]com\.csv$'
to tell grep that before com.csv
it's okay any char except for .
.
Anyway, if I can add a suggestion I wouldn't use cat
. grep can take your file as an argument, int this way:
grep -E '[^.]com\.csv$' tlds.csv
Here you can see why you should prefer not using cat
.
grep,how to search for exact pattern?
If by "exact pattern" you mean a complete line with only qtrain
in it,
then use the -x
flag:
grep -nx qtrain *.m
This will only match lines that contain exactly "qtrain" and nothing else.
If by "exact pattern" you mean to match "qtrain" but not "blahqtrain" or "qtrainblah", then you can use -w
to match whole words:
grep -nw qtrain *.m
This will match only this line in your input:
SKSC.m:195:model.qtrain = model.qtrainExtra(:,k-1);
Btw, here's another equivalent way using regular expressions:
grep -n '\<qtrain\>' *.m
From man grep
:
The symbols
\<
and\>
respectively match the empty string at the
beginning and end of a word.
How to make grep only match if the entire line matches?
Simply specify the regexp anchors.
grep '^ABB\.log$' a.tmp
Related Topics
Find The Number of Files in a Directory
How Are Interrupts Handled on Smp
Can Not Install Software in Linux Error as Dpkg Was Interrupted
How to Route Tcp/Ip Responses Through a Different Interface
Test If String Has Non Whitespace Characters in Bash
Get Subnet Mask in Linux Using Bash
Angular Cli App Not Running When Deploying to Linux App Service
Substituting a String in Place of Variable in Shell
Distro for Linux Kernel Development
Running a Script Just Before Installation of Debian Finishes Using Preseed
Vagrant Ssh -C and Keeping a Background Process Running After Connection Closed
Hidden File .Env Not Copied Using Docker Copy
How to Get The System Home Directory in Cmake on Linux
How to Provide Extend-On-Write Functionality for Memory Mapped Files in Linux
Symbol Lookup Error: ./Executablename: Undefined Symbol: _Zn18Qxmldefaulthandlerc2Ev
How to Manually Install The Eclipse-Cdt Plugin from an Archive/Zip on Ubuntu