【问题标题】:Counting number of words that contain a substring with grep用 grep 计算包含子字符串的单词数
【发布时间】:2019-09-23 21:25:02
【问题描述】:

我正在尝试查找包含“所有者”但不包含所有者本身的单词的文件。因此,例如“所有权”将被计算在内。

我知道:

grep -o -c owner  #Print only matched words.  ship(owner)ship --> owner

grep -w -c owner #Match only whole words. ownership (No), owner (Yes) 

但它仍然返回整个独立单词“所有者”。

这样做的正确方法是什么?

【问题讨论】:

  • grep -o -c owner 是否捕捉到“所有者”和派生词,例如“所有权”?如果是,减去从第二行获得的单词数,就完成了,不是吗?

标签: regex linux bash shell grep


【解决方案1】:

试试这个

grep -Pc '(\wowner)|(owner\w)' file

这个词应该有一个前缀或后缀(所以独立的不会匹配)。请注意,这将计算匹配的行数。统计出现次数

grep -oP '(\wowner)|(owner\w)' file | wc -l

【讨论】:

  • 感谢这项工作。请问P旗是做什么的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2013-07-10
  • 1970-01-01
  • 2016-12-20
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
相关资源
最近更新 更多