LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to grep for string "->" (https://www.linuxquestions.org/questions/programming-9/how-to-grep-for-string-769460/)

madpear 11-16-2009 10:25 AM

How to grep for string "->"
 
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.

fang0654 11-16-2009 10:50 AM

grep -e '->'

the -e means that the next argument is the pattern, and won't be interpreted as an argument.

bigearsbilly 11-16-2009 11:50 AM

remember, you can always use a backslash in unix
to fix such stuff,

Code:

grep \-\>  file
but WHY are you using grep in a perl script?

post what you are trying to do.

fang0654 11-16-2009 12:00 PM

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.

Robhogg 11-16-2009 02:04 PM

OK, so...

What I have gleaned is:

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


bigearsbilly 11-16-2009 02:57 PM

oops


grep '\-\>' file


All times are GMT -5. The time now is 03:38 AM.