首页 > grep被搜索字符串的首字符是‘-’该如何处理

grep被搜索字符串的首字符是‘-’该如何处理

grep用法的疑问

我在使用grep在文件中查找字符串,但是遇到类似于以-adef23n这样的字符串时,会报错,举例如下:

bash$ grep -adef23n all.id                                                                                                                                                                 
grep: invalid argument ‘ef23n’ for ‘--directories’
Valid arguments are:
  - ‘read’
  - ‘recurse’
  - ‘skip’
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

这里,它把-a识别成了命令行参数,对于这样的情况该怎么办呢?这里,我事先是不知道字符串内容的。


你看你的标题是怎么处理的


man 一下grep:

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

grep -e '-adfa' some_file

【热门文章】
【热门文章】