Grepping for exact words with UNIX
Word boundary matching is an extension to the standard POSIX grep utility. It might be available or not. If you want to search for words portably, I suggest you look into perl instead, where you would use
perl -ne 'print if /\baaa\b/' $EAT_Setup_BJ3/Log.txt
How to grep for the whole word
You want the -w
option to specify that it's the end of a word.
find . | xargs grep -sw 's:text'
Display exact matches only with grep
You need a more specific expression. Try grep " OK$"
or grep "[0-9]* OK"
. You want to choose a pattern that matches what you want, but won't match what you don't want. That pattern will depend upon what your whole file contents might look like.
You can also do: grep -w "OK"
which will only match a whole word "OK", such as "1 OK" but won't match "1OK" or "OKFINE".
$ cat test.txt | grep -w "OK"
1 OK
2 OK
4 OK
Match exact word using grep
Try this:
grep -E "^test1" file
This states that anything that starts with the word test1 as the first line. Now this does not work if you need to fin this in the middle of line but you werent very specific on this. At least in the example given the ^ will work.
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
)
grep for exact match
If you're using GNU grep then you can use the -w
or --word-regexp
flag, or wrap the regex with word boundaries:
grep '\bH\b'
However, this will be non-portable, since POSIX grep lacks the -w
flag and neither basic nor extended regular expressions support word boundaries. So if you wish for the command to be portable then you'll have to do something like
grep ' H '
or otherwise use a more powerful language, like AWK:
awk '$2=="H" { print }'
Exact Match of Word using grep
how about an awk
solution?
awk -F'|' '$1 == "CHICAGO"{print $2}' file
to only print one output, exit once you have a match, i.e.
awk -F'|' '$1 == "CHICAGO"{print $2; exit}' file
Making that more generic, you can pass in a variable, i.e.
awk -v trgt="CHICAGO" -F'|' '{targ="^" trgt " *$"; if ( $1 ~ targ ) {print $2}}' file
The " *$" regex limits the match to zero or more trailing spaces without any extra chars at the end of the target string. So this will meet your criteria to match skip matching CHICAGO PIT|BULL
.
AND this can be further reduced to
awk -v trgt="CHICAGO" -F'|' '{ if ( $1 ~ "^" trgt " *$" ) {print $2}}' file
constructing the regex "in-place" in with the comparison.
So you could use more verbose variable names to "describe" how the regex is being constructed from the input and the regex "wrappers" (as in the 3rd example) OR, you can just combine the input variable with the regex syntax in place. That is just a matter of taste or documentation conventions.
You might want to include a comment to explain you are constructing a regex test that would look like the $1 ~ /^CHICAGO *$/
.
IHTH
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.
Related Topics
Using Geom_Rect for Time Series Shading in R
Ggplot2: Splitting Facet/Strip Text into Two Lines
How to Plot a Normal Distribution by Labeling Specific Parts of the X-Axis
Use Loop to Generate Section of Text in Rmarkdown
Dplyr Without Hard-Coding the Variable Names
R Fast Single Item Lookup from List VS Data.Table VS Hash
How to Paste Together the Elements of a Vector in R Without Using a Loop
What Does the Error "Arguments Imply Differing Number of Rows: X, Y" Mean
Subset Rows According to a Range of Time
Merge Dataframes on Matching A, B and *Closest* C
How to Jitter Both Geom_Line and Geom_Point by the Same Magnitude
All Possible Combinations of a Set That Sum to a Target Value