【问题标题】:Case-insensitive search with sed [closed]使用 sed 进行不区分大小写的搜索 [关闭]
【发布时间】:2014-05-14 15:19:33
【问题描述】:

我有一个这样的文件:

This     is    my work
This is my work
This is     my    WORK

我想搜索所有模式,而不考虑空格和大小写。如何使用 sed 做到这一点?

【问题讨论】:

  • "搜索所有模式" - 你有什么模式?也许举个例子会有所帮助。

标签: shell unix sed


【解决方案1】:

您不需要sedgrep 忽略大小写和每个空格上的量词 * 绰绰有余。

grep -i 'This *is *my *work' Input.txt 

如果你坚持使用sed,可以使用this answer这个疯狂的hack,如下图。

sed  's/This *is *my *work/&/I;tx;d;:x' Input.txt

您还可以使用 jkyako 答案的以下变体。

sed -n '/This *is *my *work/Ip' Input.txt

请注意,如果您不想匹配仅包含该模式的行,而只匹配匹配该模式的行,则必须使用 @ 锚定您的模式987654328@和$

sed -n '/^This *is *my *work/Ip$' Input1.txt

【讨论】:

    【解决方案2】:

    具体使用 sed 你可以尝试以下方法,也许有更短的方法:

    sed -n '/[Tt][Hh][Ii][Ss] *[Ii][Ss] *[Mm][Yy] *[Ww][Oo][Rr][Kk]/p'
    

    【讨论】:

      【解决方案3】:

      抱歉,我的原始答案中没有空格。 这应该可以解决这个问题。

      grep -i "this\s*is\s*my\s*work" file
      

      【讨论】:

      • ...这就是为什么我建议使用 grep 而不是 sed :)
      猜你喜欢
      • 1970-01-01
      • 2012-12-20
      • 2010-09-12
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多