在grep中使用指定量词检索满意的词汇

在grep中使用指定量词检索满意的词汇

我尝试从文件中抓取单词:

$ 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**

我想要的结果是只得到文字:

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

使用指定限定符重构代码{,20}

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

不幸的是,它什么也没返回。

遇到这样的问题怎么解决呢?

答案1

对于 BSD grep,请参阅 参考资料man 7 re_format来了解支持的正则表达式的详细信息。它特别指出:

 A bound is `{' followed by an unsigned decimal integer, possibly followed
 by `,' possibly followed by another unsigned decimal integer, always fol-
 lowed by `}'.  The integers must lie between 0 and RE_DUP_MAX (255=)
 inclusive, and if there are two of them, the first may not exceed the
 second.

仅第二个数字可以省略;首先给予。

通过该修复:

$ /usr/bin/grep --version
grep (BSD grep) 2.5.1-FreeBSD
$ /usr/bin/grep -Eo '\*\*[^*]{0,20}\*\*' foo
**circumstance**
**case**
**condition**
**Anxiety**
**anxiety**

相关内容