Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I grep for a string that begins with a dash/hyphen?

I want to grep for the string that starts with a dash/hyphen, like -X, in a file, but it's confusing this as a command line argument.

I've tried:

grep "-X" grep \-X grep '-X' 
like image 528
Mike Avatar asked Mar 11 '10 19:03

Mike


People also ask

How do you grep with a dash?

An alternative you can use instead of the double dash is -e . For example, grep -w -e -r (or grep -we -r ) would have the same result as grep -w -- -r in the example above. -e allows specifying a search pattern, and it can be used to protect a pattern beginning with a dash ( - ).

How do you grep words with special characters?

If you include special characters in patterns typed on the command line, escape them by enclosing them in single quotation marks to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to grep –E, put a backslash ( \ ) in front of the character.

How do you grep words in a string?

To find a pattern that is more than one word long, enclose the string with single or double quotation marks. The grep command can search for a string in groups of files. When it finds a pattern that matches in more than one file, it prints the name of the file, followed by a colon, then the line matching the pattern.


1 Answers

Use:

grep -- -X

Related: What does a bare double dash mean? (thanks to nutty about natty).

like image 163
newacct Avatar answered Oct 09 '22 08:10

newacct