ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
It seems easy, but I can't seem to do a `grep "->" input`. I would like to be able to do this within backticks in perl where the input for grep is stored in a variable $variable or @variable e.g. `@variable | grep "->"`.
The error I am currently getting is because no matter how I specify ->, grep thinks I am trying to specify the option -> which is, of course, not valid.
EDIT: I found that in my case, I only needed to use ">", however, I am still interested in how to grep for a pattern starting with a "-". Also, whenever I use @variable within backticks in perl, even if I follow it with a pipe, it tries to execute all the lines of that variable as commands instead of piping them to the grep/awk/etc.
For whatever reason, grep takes \- as an argument just as much as it takes - or "-". So the example you posted:
Code:
grep \-\> file
still errors out with:
Code:
grep: invalid option -- '>'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
Unless I'm mistaken (which is always a distinct possibility!), the \ only makes sure the character gets past the shell, and actually gets passed to the program, instead of being interpreted by the shell. For example,
Code:
echo This is a & test
Will interpret the & and split the line into two distinct processes, where
Code:
echo This is a \& test
will pass "This is a & test" to the echo command. So as far as grep is concerned, - and \- are the same thing.
The backslash is interpreted by the shell as escaping the next character, which then gets passed literally to the program (without the backslash). So:
Code:
# Both the '-' and '>' are escaped in the shell, but the '-' is passed unescaped
# to grep, which interprets -> as an invalid option
rob@rob-laptop:~$ echo $var | grep \-\>
grep: invalid option -- >
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
# The '\' is escaped in the shell, so '\->' gets passed to grep.
# It interprets the pattern correctly.
rob@rob-laptop:~$ echo $var | grep \\-\>
$this->cheese
# The '>' is now no longer escaped in the shell,
# so bash throws an error
rob@rob-laptop:~$ echo $var | grep \\-\\>
bash: syntax error near unexpected token `newline'
# The quotes protect the pattern from the shell, but not from grep
rob@rob-laptop:~$ echo $var | grep '->'
grep: invalid option -- >
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
bash: echo: write error: Broken pipe
# The quotes protect the '>', the backslash then escapes the '-' for grep
rob@rob-laptop:~$ echo $var | grep '\->'
$this->cheese
# '--' means "no more options"
rob@rob-laptop:~$ echo $var | grep -- '->'
$this->cheese
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.