Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep for a string that includes "->"

Tags:

c++

grep

I am looking for the literal string ->foo in all the *.cpp files in a single directory. If I try

grep -F "->foo" *.cpp

grep reports

Invalid option -- '>'

Then, if I try

grep -F "-\>foo" *.cpp

I get

Invalid option -- '\'

How can I get this working?

like image 936
Amittai Aviram Avatar asked Feb 21 '26 10:02

Amittai Aviram


2 Answers

Generally (not grep specific) using -- signifies the end of options:

grep -F -- "->foo" *.cpp

Helpful when you accidentally create files starting with -:

$ touch -- -damn 

$ ls -- -*
-damn

$ rm -damn
rm: invalid option -- 'd'

$ rm -- -damn
like image 174
Chris Seymour Avatar answered Feb 23 '26 23:02

Chris Seymour


Try this:

grep -e "->foo" *.cpp

From the man page:

-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 .) [emphasis added]

like image 33
Robᵩ Avatar answered Feb 23 '26 23:02

Robᵩ