LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-16-2009, 10:25 AM   #1
madpear
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Rep: Reputation: 0
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.

Last edited by madpear; 11-16-2009 at 10:39 AM.
 
Old 11-16-2009, 10:50 AM   #2
fang0654
Member
 
Registered: Oct 2003
Location: New York, NY
Distribution: Ubuntu
Posts: 110

Rep: Reputation: 28
grep -e '->'

the -e means that the next argument is the pattern, and won't be interpreted as an argument.
 
Old 11-16-2009, 11:50 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.

Last edited by bigearsbilly; 11-16-2009 at 11:51 AM.
 
Old 11-16-2009, 12:00 PM   #4
fang0654
Member
 
Registered: Oct 2003
Location: New York, NY
Distribution: Ubuntu
Posts: 110

Rep: Reputation: 28
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.

Last edited by fang0654; 11-16-2009 at 12:01 PM.
 
Old 11-16-2009, 02:04 PM   #5
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
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

Last edited by Robhogg; 11-16-2009 at 02:19 PM.
 
Old 11-16-2009, 02:57 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
oops


grep '\-\>' file
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
Problem "$value=`mpstat 1 1 | grep "Average"`;" Alias pipe return nothing adamlucansky Linux - General 8 09-25-2009 07:26 AM
Perl - Can't use string ("html") as an ARRAY ref while "strict refs" OldGaf Programming 9 08-11-2009 11:14 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration