【问题标题】:Bash grep command starts with or ends withBash grep 命令以
【发布时间】:2014-06-11 00:55:30
【问题描述】:

我正在执行一个命令,该命令将根据以给定模式开头或结尾的模式匹配返回结果。

这是我目前所拥有的。

"cat input.txt | grep "^in|in$"

我的主要问题是我无法让(或)工作,但我可以让他们单独工作。

提前感谢您的帮助。

【问题讨论】:

  • 看起来是你的朋友不久前问了this

标签: bash unix terminal grep


【解决方案1】:

这个awk 应该可以工作:

awk '/^start|end$/' file

它将打印所有以start 开头或以end 结尾的行

cat file
nothing
start with this
or it does have an end
or the end is near

awk '/^start|end$/' file
start with this
or it does have an end

【讨论】:

    【解决方案2】:

    试试这个:

    grep "^in\|in$" input.txt
    

    默认情况下,grep 使用BRE,你必须转义|。或者使用grep的-E or -P,以免转义那些有特殊含义的char。

    P.S,cat 是没有必要的。

    【讨论】:

      【解决方案3】:

      您是否想过使用 egrep 而不是 grep?使用以下内容应该可以满足您的需求:

      egrep "^in|in$" input.txt
      

      一开始不需要猫,上面的就可以了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-31
        • 1970-01-01
        • 1970-01-01
        • 2021-10-30
        • 2013-02-28
        • 2012-05-04
        • 2018-10-17
        • 1970-01-01
        相关资源
        最近更新 更多