How to Grep for The Exact Word If The String Has Got Dot in It

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



Leave a reply



Submit