包含“-c”的grep

时间:2013-04-02 12:10:50

标签: bash shell ksh

我想使用-c

搜索grep

例如:

$>ls -al | grep '-c'

grep认为这是一种选择。

$>Usage: grep -hblcnsviw pattern file . . .

如何将-c搜索为字符串?

4 个答案:

答案 0 :(得分:6)

告诉grep你的选项结束的地方:

grep -- -c

答案 1 :(得分:2)

你可以:

  1. 逃离连字符。

    grep '\-c'
    
  2. 使用-e--regexp=)标记

    grep -e -c
    grep --regexp=-c    # Not in POSIX, but supported at least in Linux and OS X.
    

答案 2 :(得分:1)

您使用-e选项:

$ ls -a1 | grep -e -c

当然这是in the documentation,因此:

  

-e PATTERN, - regexp = PATTERN

     

使用PATTERN作为模式。这可用于指定多个搜索模式,或用于保护以连字符( - )开头的模式。 (-e由POSIX指定。)

答案 3 :(得分:0)

要继续讨论主题,原始问题的答案是使用--表示要处理的选项的结束,或使用-e明确标记该选项。

但是,如果任何文件名包含新行,则解析ls的输出将产生不正确的结果。请改用find

find . -depth 1 -name "-c"