如何grep'---';在Linux中?grep:无法识别的选项'---';

如何grep'---';在Linux中?grep:无法识别的选项'---';,linux,shell,ubuntu,grep,ubuntu-10.04,Linux,Shell,Ubuntu,Grep,Ubuntu 10.04,我有一个新安装的web应用程序。其中有一个下拉列表,其中一个选项是--。我想做的是将其更改为All。所以我导航到应用程序文件夹并尝试了下面的命令 grep -ir '---' . 我最后犯了以下错误 grep: unrecognized option '---' Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. 考虑到我正在使用 Distributor ID: Ubuntu De

我有一个新安装的web应用程序。其中有一个下拉列表,其中一个选项是
--
。我想做的是将其更改为
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'--'呢?

之所以发生这种情况,是因为
grep
--
解释为一个选项,而不是要查找的文本。相反,请使用
--

这样,您就可以告诉grep
rest不是命令行选项

其他选择:

  • 使用
    grep-e
    (见我在他发布时添加的,直到现在才注意到):

  • 使用
    awk
    (请参阅)或
    sed

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

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

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

grep '\-\-\-' your_file
仅转义第一个
-
也有效:

grep '\---' your_file
不带引号的备选方案:

grep \\--- your_file
或者您可以使用awk:

awk '/---/' file
或sed:

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

使用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.)
保护以连字符(-)开头的图案。

   -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.)