-1

I tried to use the grep command to search for a string like -R in a file. But the command thinks, that I am trying to hand over an option like -i for ignore case.

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Adrian
  • 1
  • 1
  • i think that you wrote the answer in your question ... use `"-R"` , not `-R` – jsotola Jun 09 '19 at 21:58
  • @jsotola are you serious? to the op: use `grep -e -R ./-file1 file2`, where `-R` is a pattern starting with `-`, and `./-file1` is a filename starting with `-`. –  Jun 09 '19 at 21:59
  • 4
    The answer is here: [What does “--” (double-dash) mean? (also known as “bare double dash”)](https://unix.stackexchange.com/a/11382/332764) – Freddy Jun 09 '19 at 22:01
  • Thank you, @Freddy and pizdelect, both of your answers work for me! – Adrian Jun 09 '19 at 22:13

3 Answers3

3

The -e option for grep is for explicitly saying "the next argument is the pattern":

grep -e -R file

The above would search for line matching -R in the file called file.

The -e option may occur multiple times on the command line, and grep will use all the given patterns (you will get the lines back that matches any of the patterns).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
2

As well as the previously mentioned answers, -- is used to signify the end of options, allowing you to use patterns afterward that may resemble an argument without being interpreted undesirably:

$ echo -R | grep -- '-R'
-R
jesse_b
  • 35,934
  • 12
  • 91
  • 140
1

Try to escape the characters grep \\-R myfile.txt:

Example:

$ touch test.txt
$ echo "-R" >> ./test.txt
$ cat test.txt
-R
$ grep \\-R ./test.txt
-R
JJbh
  • 44
  • 2