[SOLVED] Exclamation point in Grep for Negated Lookbehind: How to Do it?
Linux - NewbieThis 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
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.
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?
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 02:24 PM.
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!
@ 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.