Ubuntu – Use specified quantifier in grep to retrieve satisfied vocabulary

command linetext processing

I'm trying to grab words from a file:

$ grep -o '\*\*[^*]*\*\*' Principles_20_LifePrinciples.md | grep -v -e "Origin" -e "Etymology"
**circumstance**
**case**
**condition**
**Anxiety**
**anxiety**
**the state of feeling nervous or worried that sth bad is going to happen**
**a worry or fear about sth**
**a strong feeling of wanting to do sth or of wanting sth to happen**

The result I intend is to get only words:

**circumstance**
**case**
**condition**
**Anxiety**
**anxiety**

Refactored code with specified quantifiers {,20}:

$ grep -E -o '\*\*[^*]{,20}\*\*' Principles_20_LifePrinciples.md

Unfortunately, it returns nothing.

How to solve such a problem?

Best Answer

  • Note that GNU grep supports \w for matching word characters (alphabets, digits and underscore), so you could easily match only word characters in the part between the asterisks:

    grep -o '\*\*\w*\*\*'
    
  • Related Question