LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2010, 03:13 PM   #1
mattseanbachman
Member
 
Registered: Feb 2010
Posts: 40

Rep: Reputation: 15
Exclamation point in Grep for Negated Lookbehind: How to Do it?


I'm trying to work with lookaround using grep with the -P switch. Here's where the problem lies:
Code:
grep -P "wall(?=street)" ./words 
wallstreet

grep -P "wall(?!street)" ./words 
bash: !street: event not found
In the first case, everything works great. But when I try to do negative lookahead, i.e. if street is right after wall, discard the result, it doesn't work.

I've tried escaping the exclamation point. Here's what that does:
Code:
grep -P "wall(?\!street)" ./words 
grep: unrecognized character after (? or (?-
I've also Googled this and checked forums; this has probably been covered somewhere else but I am at a loss to find where.

Anyone have any comments on why it's doing this or how to fix it?
 
Old 03-15-2010, 03:23 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Seems to work for me by using single quotes around the thing you're grepping:

Code:
root@reactor: echo 'wallstreet' | grep -P 'wall(?=street)'
wallstreet

root@reactor: echo 'wallstreet' | grep -P 'wall(?!street)'
root@reactor:
Does that seem right? Grep acts differently when things are double-quoted (I'm sure there's a fully-detailed explanation, with lots of examples, but rather than diagnose it, I usually just switch to single quotes and try that. )

Sasha

Last edited by GrapefruiTgirl; 03-15-2010 at 03:24 PM.
 
Old 03-15-2010, 03:24 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
From the GREP man page:
Quote:
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression.
This is highly experimental and grep -P may warn of
unimplemented features.
For me at least, that says: "try a different approach." In this case maybe just use Perl?
 
Old 03-15-2010, 03:27 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Yes, or use two instances of grep, connected with a pipe (klunky maybe, but whatever you like):

Code:
echo 'wallstreet' | grep wall | grep -v wallstreet
Sasha
 
Old 03-15-2010, 03:29 PM   #5
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS
Posts: 179

Rep: Reputation: 27
Hi,

also this way should work.

x='!street'; grep -P "wall(?${x})" ./words
 
Old 03-15-2010, 08:42 PM   #6
mattseanbachman
Member
 
Registered: Feb 2010
Posts: 40

Original Poster
Rep: Reputation: 15
Thanks for everybody's help. All solutions (from GrapefruiTgirl and mario.almeida) worked.

This might very well be a case of the -P switch being something to be careful with...however, this is the first bug that I've encountered using it thusfar. Mostly when I posted this I thought it was a syntax problem or what-not. Glad to know I wasn't out of my mind!

Again, thanks for the help everyone.
 
Old 03-15-2010, 09:10 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 15,045

Rep: Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914Reputation: 1914
Quote:
Originally Posted by GrapefruiTgirl View Post
Does that seem right? Grep acts differently when things are double-quoted
That's interpolation for you ...
Its everywhere - try this
Code:
echo  'fred\\\ngeorge' ; echo "fred\\\ngeorge"
 
Old 03-15-2010, 09:45 PM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
@ syg00 -- yep, I understand, but I don't believe interpolation is the right word for it (not to my understanding if the word) but that's neither here nor there --

Quote:
in·ter·po·late  /ɪnˈtɜrpəˌleɪt/ [in-tur-puh-leyt]
–verb (used with object) -- to cause oneself (object) to become the subject of an Interpol investigation. To interpolate oneself..
I thought it was more simple- maybe 'shell interpretation' or something like this..

@ mattseanbachman, it's not a bug; more of a behavior. The different types of quotes cause the shell to interpret, expand, ignore, etc., the stuff within the quotes.

Anyhow, on with the show!
 
  


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
Server Indicate Exclamation mark (!) Prabuddha Linux - Hardware 1 06-15-2009 08:43 AM
Exclamation The smsc9115 driver can't receive normally kine920 Linux - Embedded & Single-board computer 3 07-18-2008 12:24 AM
Exclamation point in disk path? (i.e. /dev/cciss!c0d0p3) jchrisos Fedora 0 07-18-2006 12:04 PM
how do you disable red exclamation point (up2date) in the gnome toolbar? harry349 Red Hat 3 05-26-2004 09:03 AM
Shell scripting: exclamation marks in strings Dark_Helmet Programming 9 06-16-2003 07:32 PM


All times are GMT -5. The time now is 01:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration