Grep,How to Search for Exact Pattern

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

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 can i search an exact string using Grep?

You can search for exact word using -w option.
Use

grep -w string

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

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 an exact string with slash in it?

You may use

grep -E '(^|/)superman($|/)' file

See the online demo:

s="/superman
/superman1
/superman/batman
/superman2/batman
/superman/wonderwoman
/superman3/wonderwoman
/batman/superman
/batman/superman1
/wonderwoman/superman
/wonderwoman/superman2"
grep -E '(^|/)superman($|/)' <<< "$s"

Output:

/superman
/superman/batman
/superman/wonderwoman
/batman/superman
/wonderwoman/superman

The pattern matches

  • (^|/) - start of string or a slash
  • superman - a word
  • ($|/) - end of string or a slash.

grep matches exact string or with wildcard

grep uses regex for pattern matching. grep -w 'name1*' would make it match zero or more 1s, so name1 and name11 would match.

If it only matches name1 for you it's because you have a file named name1 in the directory and the unquoted name1* will be interpreted by the shell (globbing). Always use quotes around your arguments that contain special characters. If you use a variable, always put " around it.

To make it match any name starting with name1, make it

grep -w 'name1.*' filename
  • . means "any character"
  • .* means "any character, zero or more times".

If the input comes from some external source where * is used as a wildcard, you need to change that string before calling grep.

Example:

search_str='name1*'
new_search_str="$(sed 's/\*/.*/g' <<< "$search_str")"
grep -w "$new_search_str" filename

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 exact pattern from a file in bash

Use the following command:

grep -owF "3.3.3.1" tesfile

-o returns the match only and not the whole line.-w greps for whole words, meaning the match must be enclosed in non word chars like <space>, <tab>, ,, ; the start or the end of the line etc. It prevents grep from matching 3.3.3.1 out of 3.3.3.111.

-F greps for fixed strings instead of patterns. This prevents the . in the IP address to be interpreted as any char, meaning grep will not match 3a3b3c1 (or something like this).



Related Topics



Leave a reply



Submit