【问题标题】:Grep not Giving expected result while using pipe使用管道时Grep没有给出预期的结果
【发布时间】:2021-06-03 11:33:58
【问题描述】:

我在命令下运行,但它返回空。

grep -i -e "abc()|daf()|newname|" test.txt

test.txt 包含以下数据。

abc()
daf()
blahblah
newname

我的输出为空,但希望返回字符串,有人可以解释或分享为什么没有得到所需的输出。谢谢!!

【问题讨论】:

    标签: linux grep


    【解决方案1】:

    来自grep(1)man page

    基本与扩展正则表达式

    在基本的正则表达式中,元字符?、+、{、|、(和)失去了它们的特殊含义;而是使用反斜杠版本?、+、{、|、(和)。

    有 2 个选项;

    1. 使用扩展的正则表达式 -E 来“启用”|

      grep -i -E "abc()|daf()|newname" test.txt
      

      What is the difference between grep -e and grep -E option?

    2. 转义 | 以继续使用常规正则表达式 -e

      grep -i -e "abc()\|daf()\|newname" test.txt
      

    这两个选项都会导致:

    abc()
    daf()
    newname
    

    Try it online!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-09
      • 1970-01-01
      相关资源
      最近更新 更多