【问题标题】:How to grep '---' in Linux? grep: unrecognized option '---'如何在 Linux 中 grep '---'? grep:无法识别的选项'---'
【发布时间】:2014-10-16 02:10:37
【问题描述】:

我有一个新安装的网络应用程序。在那里有一个下拉菜单,其中一个选项是---。我想做的是将其更改为All。所以我导航到应用程序文件夹并尝试了以下命令。

grep -ir '---' .

我最终得到以下错误。

grep: unrecognized option '---'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

鉴于我正在使用

Distributor ID: Ubuntu
Description:    Ubuntu 10.04.4 LTS
Release:    10.04
Codename:   lucid

如何在 Linux 中 grep '---' ?

【问题讨论】:

    标签: linux shell ubuntu grep ubuntu-10.04


    【解决方案1】:

    这是因为grep--- 解释为一个选项而不是要查找的文本。相反,请使用--:

    grep -- "---" your_file
    

    这样,您告诉grep 其余的不是命令行选项。

    其他选项:

    • 使用grep -e(见Kent's solution,因为我在他已经发布时添加了它 - 直到现在才注意到它):

    • 使用awk(见anubhava's solution)或sed

      sed -n '/---/p' file
      

    -n 阻止sed 打印行(其默认操作)。然后/--- 匹配那些包含---/p 的行使它们被打印出来。

    【讨论】:

      【解决方案2】:

      另一种方法是使用反斜杠转义每个 -

      grep '\-\-\-' your_file
      

      只转义第一个 - 也可以:

      grep '\---' your_file
      

      不带引号的替代方案:

      grep \\--- your_file
      

      【讨论】:

        【解决方案3】:

        或者你可以使用awk:

        awk '/---/' file
        

        或 sed:

        sed -n '/---/p' file
        

        【讨论】:

          【解决方案4】:

          使用 grep 的 -e 选项,它是满足您要求的正确选项:

             -e PATTERN, --regexp=PATTERN
                    Use PATTERN as the pattern.  This can be used to specify multiple search patterns, or to protect a pattern beginning with a hyphen (-).  (-e is specified
                    by POSIX.)
          

          保护以连字符 (-) 开头的模式

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-04-18
            • 2015-02-16
            • 1970-01-01
            • 1970-01-01
            • 2015-07-02
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多