【问题标题】:How to exclude hyphen as word separator in bash如何在bash中排除连字符作为单词分隔符
【发布时间】:2016-04-19 10:47:26
【问题描述】:

我无法grep 获取包含连字符的精确单词匹配,如

/home/imper-home,3,0,0,0,jim.imper,NONE,NONE,NONE,http://sanjose                                                                                                                                 
/home/imper,15,10,3,30,jim.imper,NONE,NONE,NONE,http://sanjose-age

我试过了

grep -w imper  

但它同时返回 /home/imper-home 和 /home/imper。

我只希望 /home/imper-home 通过使用返回,

grep -wv /home/imper

【问题讨论】:

  • grep 'imper-' file ?
  • 我通常需要它来用于任何变量,而不仅仅是“imper”。在 grep 中排除单词匹配中的连字符
  • 如果您可以访问p 标志,为什么不直接使用grep -P '(/home/imper)(?=,)' filename --color。关于我提到的答案,我只是想让您了解- 的问题。
  • 如果@123 的回答对你不起作用,说明问题不够清楚。此外,您似乎正在尝试解决 x,但要求 y 是错误的开始方法。

标签: bash shell scripting grep solaris


【解决方案1】:

这通常会起作用:

word=imper
grep -w "$word" file | grep -v "$word-"

【讨论】:

    猜你喜欢
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多