1

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?

jww
  • 97,681
  • 90
  • 411
  • 885
newID
  • 141
  • 1
  • 7
  • @WiktorStribiżew, you are too fast. I intended to ask about the special character. the \b does not work. Can you take a look? – newID Jun 16 '19 at 20:17

2 Answers2

2

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
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

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 '*'.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85