指定された数量子を使用して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.

最初の数字は2番目の数字だけを省略できます。持つ与える。

この修正を使用すると、次のようになります。

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

おすすめ記事