ChatGPT解决这个技术问题 Extra ChatGPT

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'

T
T.J. Crowder

Use:

grep -- -X

Documentation

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


for those of us wondering at this stage what -- means or does: unix.stackexchange.com/questions/11376/…
Works with The Silver Searcher (ag) as well.
If you want to use with variable having spaces you should use something like this: ARGS="-a -b -c" grep -- "$ARGS"
@zapstar Or just, grep -- "--name awesome".
@BrettWidmeier, yes, I should not have used the variable, it confuses people.
c
codeforester

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.


I don't think - is a special character in Bash despite what the ABS guide says. Bash doesn't process arguments beginning with - any differently than others. The command or built-in that Bash subsequently calls will handle arguments beginning with a dash however it sees fit. Most of the Bash built-ins and Linux and GNU commands handle -- as an option terminator. Use 'set -x' to see the commands and arguments that Bash uses before they will be executed, but after Bash has expanded escapes, globs, and braces. Grep is a confusing example because it does its own handling of backslash escapes.
T
Thomas

grep -e -X will do the trick.


my grep doesn't have -e, very weird
Available on solaris from /usr/xpg4/bin/grep, btw. Lots of more-complete utilities in that directory.
It worked under MinGW (MinGW doesn't support -P (but that is another story and does not apply here)).
2
2 revs, 2 users 77%
grep -- -X
grep \\-X
grep '\-X'
grep "\-X"
grep -e -X
grep [-]X

e
ezpz

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


f
fmmarques

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.


C
Colin vH

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

'[-]X'

g
ghostdog74

you can use nawk

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

P
Pang
ls -l | grep "^-"

Hope this one would serve your purpose.


This expression catch only lines that begin with a dash. It does not complies with the initial question. He want to search for a string starting by a dash but the lines that contains this string did not necessary begin with a dash.In the initial question, the string beginning with a dash can be anywhere in the line.
s
sasank

grep "^-X" file

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


There’s already an answer which suggested that stackoverflow.com/a/27440348/441757 (posted more than a year ago)
The OP is looking for strings that starts with -, not lines.