2

I wanted to find out the list of java files, where I have used lambda expressions. Towards this, I wanted to grep for the expression "->". Some of the queries similar to mine got the response that grep should be used with the -F option. When I did this like:

grep -F "->" *.java

I got the error message:

grep: invalid option -- >

I am using MacOS Darwin.

Seshadri R
  • 1,192
  • 14
  • 24

5 Answers5

5

Put double hyphen before your keyword:

grep -- '->' *.java
3

POSIX specifies the -e option. It can be used to specify multiple patterns, but it’s also useful for a single pattern that begins with a hyphen:

grep -e '->' *.java
Biffen
  • 6,249
  • 6
  • 28
  • 36
2

When you have 'unsafe' patterns, better the follow the pattern. It will reduce the change of parsing error - it clearly separate options from the pattern

grep [options] -- "REGEXP" files

In this case

grep -F -- "->" *.java
dash-o
  • 13,723
  • 1
  • 10
  • 37
1

You can use \ to escape the - character, which is interpreted as an indicator for a command line argument if you don't escape it: grep "\->" *.java

eike
  • 1,314
  • 7
  • 18
  • **grep -F "\->" *.java** does not produce any output or error message. But, **grep "\->" *.java** gives the expected result. – Seshadri R Nov 19 '19 at 09:18
  • @SeshadriR sorry, I didn't even look into the `-F` option. I just assumed that wasn't part of your problem. – eike Nov 19 '19 at 09:20
1

What you always can do is to encapsulate the weird symbols in regular expression brackets ([]).

$ grep '[-][>]' file

This is what I would do in this case of the OP. There is a problem, the OP is now sure what the problem is, is it the hyphen (-) or the greater-than sign (>), encapsulate!

The problem here is, however, the hyphen. Hyphens generally signify a command/utility option. The utility grep sees that one of its arguments starts with a hyphen, says "Hey, here comes an option, lets process it". It checks the option, sees >, and says "Sorry, no can do!"

For reasons, like the above, the Posix standard introduced the double-hypen with the following requirement:

The requirement for recognizing -- is because conforming applications need a way to shield their operands from any arbitrary options that the implementation may provide as an extension. For example, if the standard utility foo is listed as taking no options, and the application needed to give it a pathname with a leading <hyphen-minus>, it could safely do it as:

foo -- -myfile

and avoid any problems with -m used as an extension.

source: POSIX.1-2017 Utility Description Defaults

Posix.1-2017 also makes this a guideline for when you write your own utilities:

Guideline 10: The first -- argument that is not an option-argument should be accepted as a delimiter indicating the end of options. Any following arguments should be treated as operands, even if they begin with the - character.

source: POSIX.1-2017 Utility Syntax Guidelines

An implementation of this guideline can be seen in BashFAQ 035.

Hence, the true solution to the problem would have been:

$ grep -- '->' file
Community
  • 1
  • 1
kvantour
  • 25,269
  • 4
  • 47
  • 72