【问题标题】:Cut the GREP command in Linux在 Linux 中剪切 GREP 命令
【发布时间】:2012-06-15 17:37:51
【问题描述】:

好的,所以我有一个名为 search 的变量,其中包含字符串“Find this Code - Now”。我想搜索该代码,如果找到它,系统会在这些行上回复 true 或其他内容,如果没有,则它只是退出脚本并出错。

我尝试使用 grep,但我不知道如何仅剪切搜索的内容,因此我可以运行 if else 语句

【问题讨论】:

    标签: linux shell unix scripting grep


    【解决方案1】:

    您可以使用-q 开关并检查grep 的退出状态,即类似grep -q $var <file> && echo "true"

    【讨论】:

      【解决方案2】:

      我尝试使用 grep,但我不知道如何仅剪切搜索的内容,因此我可以运行 if else 语句

      grep 有一个开关--only-matching

       -o, --only-matching       show only the part of a line matching PATTERN
      

      【讨论】:

        【解决方案3】:
        > X="not gonna find it"
        > grep -qR "$X" .; echo $?
        1
        > X="Find this Code - Now"
        > grep -qR "$X" .; echo $?
        0
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-31
          • 2015-07-21
          • 1970-01-01
          • 2013-11-21
          • 1970-01-01
          • 2013-05-18
          • 1970-01-01
          • 2013-01-22
          相关资源
          最近更新 更多