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

UnixGrepEscaping

Unix Problem Overview


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'

Unix Solutions


Solution 1 - Unix

Solution 2 - Unix

The dash is a special character in Bash as noted at http://tldp.org/LDP/abs/html/special-chars.html#DASHREF. So escaping this once just gets you past Bash, but Grep still has it's own meaning to dashes (by providing options).

So you really need to escape it twice (if you prefer not to use the other mentioned answers). The following will/should work

grep \\-X
grep '\-X'
grep "\-X"

One way to try out how Bash passes arguments to a script/program is to create a .sh script that just echos all the arguments. I use a script called echo-args.sh to play with from time to time, all it contains is:

echo $*

I invoke it as:

bash echo-args.sh \-X
bash echo-args.sh \\-X
bash echo-args.sh "\-X"

You get the idea.

Solution 3 - Unix

grep -e -X will do the trick.

Solution 4 - Unix

grep -- -X
grep \\-X
grep '\-X'
grep "\-X"
grep -e -X
grep [-]X

Solution 5 - Unix

I dont have access to a Solaris machine, but grep "\-X" works for me on linux.

Solution 6 - Unix

The correct way would be to use "--" to stop processing arguments, as already mentioned. This is due to the usage of getopt_long (GNU C-function from getopt.h) in the source of the tool.

This is why you notice the same phenomena on other command-line tools; since most of them are GNU tools, and use this call,they exhibit the same behavior.

As a side note - getopt_long is what gives us the cool choice between -rlo and --really_long_option and the combination of arguments in the interpreter.

Solution 7 - Unix

If you're using another utility that passes a single argument to grep, you can use:

'[-]X'

Solution 8 - Unix

you can use nawk

$ nawk '/-X/{print}' file

Solution 9 - Unix

ls -l | grep "^-"

Hope this one would serve your purpose.

Solution 10 - Unix

grep "^-X" file

It will grep and pick all the lines form the file. ^ in the grep"^" indicates a line starting with

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeView Question on Stackoverflow
Solution 1 - UnixnewacctView Answer on Stackoverflow
Solution 2 - UnixChad GorshingView Answer on Stackoverflow
Solution 3 - UnixThomasView Answer on Stackoverflow
Solution 4 - UnixJohn HenryView Answer on Stackoverflow
Solution 5 - UnixezpzView Answer on Stackoverflow
Solution 6 - UnixfmmarquesView Answer on Stackoverflow
Solution 7 - UnixColin vHView Answer on Stackoverflow
Solution 8 - Unixghostdog74View Answer on Stackoverflow
Solution 9 - UnixXINUView Answer on Stackoverflow
Solution 10 - UnixsasankView Answer on Stackoverflow