File:
cat test.txt
100 ***
10 ** // target
Grep:
grep -E '\*{2}' test.txt
100 ***
10 **
Isn't the {m}
used to do exact the number of the match?
How to get the *
with exact 2 occurrence?
File:
cat test.txt
100 ***
10 ** // target
Grep:
grep -E '\*{2}' test.txt
100 ***
10 **
Isn't the {m}
used to do exact the number of the match?
How to get the *
with exact 2 occurrence?
You may use
grep -E '(^|[^*])\*{2}($|[^*])' test.txt
See online grep
demo:
test="100 ***
10 ** // target"
grep -E '(^|[^*])\*{2}($|[^*])' <<< "$test"
# => 10 ** // target
Details
(^|[^*])
- start of string or any char but *
\*{2}
- two asterisks($|[^*])
- end of string or any char but *
.Variation with whitespace boundaries
If you need to only match a "word" in between spaces use
grep -E '(^|[[:space:]])\*{2}($|[[:space:]])' test.txt
A simple basic regular expression will work as well explicitly specifying "**"
not surrounded by a '*'
. For example:
$ grep '^[^*]*[*][*][^*]*$' test.txt
10 **
That will match the line containing [*][*]
anywhere in the line not surrounded by another '*'
.