1

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?

8
  • The last grep command worked for me. Which version of Ubuntu are you using and what is the output of grep --version?
    – muru
    Mar 27, 2018 at 5:55
  • grep (BSD grep) 2.5.1-FreeBSD @muru
    – AbstProcDo
    Mar 27, 2018 at 5:57
  • If you're not using Ubuntu, you should post the question on Unix & Linux instead.
    – muru
    Mar 27, 2018 at 5:58
  • Ubuntu 17.10 @muru
    – AbstProcDo
    Mar 27, 2018 at 6:00
  • 1
    Duplicate of askubuntu.com/q/1019399/295286 Mar 27, 2018 at 10:00

2 Answers 2

2

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*\*\*'
0

You're almost there with your first grep command.

If you only need to match line that have a single word, add a space in the [^...] regex:

$ grep -o '\*\*[^* ]*\*\*' file
**circumstance**
**case**
**condition**
**Anxiety**
**anxiety**

Not the answer you're looking for? Browse other questions tagged or ask your own question.