2

I want to find a word in strings, but only if it doesn't begin with a prefix.

for example.

I'd like to find all the appearances of APP_PERFORM_TASK, but only if they are not starting with a prefix of CMD_DO("

so,

CMD_DO("APP_PERFORM_TASK")   <- OK (i don't need to know about this)  
BLAH("APP_PERFORM_TASK")     <-- NOT OK, this should match my search.

I tried:

(?!CMD_DO\(")APP_PERFORM_TASK

But that doesn't produce the results I need. What I doing wrong?

7
  • What language / engine? Commented Oct 7, 2013 at 19:22
  • I'm just looking for the regex expression. nothing particular. Commented Oct 7, 2013 at 19:24
  • But what you have appears to work: rubular.com/r/eKNxWoSLf8 Commented Oct 7, 2013 at 19:26
  • 1
    And alternatively, possible duplicate of this, this, this, this, this ... Commented Oct 7, 2013 at 19:30
  • @ExplosionPills Lol, add the i flag and see what happens ;) Commented Oct 7, 2013 at 19:55

6 Answers 6

1

Here's a quick way:

Use the --invert-match (also known as -v) flag to ignore CMD_DO and pipe the results to a second grep that only matches BLAH:

grep -v CMD_DO dummy | grep BLAH
Sign up to request clarification or add additional context in comments.

Comments

1

Try replacing NegativeLookAhead (?!) with NegativeLookBehind (?<!) in your regex

(?<!CMD_DO\(")APP_PERFORM_TASK

Check this in action here

Comments

1

Based on your comment: Let's concentrate on command line tool grep

Here is grep solution without using -P switch (perl like regex):

grep 'APP_PERFORM_TASK' file | grep -v '^CMD_DO("'

Here is grep solution using -P switch and negative lokbehind:

grep -P '(?<!^CMD_DO\(")APP_PERFORM_TASK' file

Comments

0

Try this

(?!CMD_DO\(").*APP_PERFORM_TASK.*

Comments

0

To handle an input line with both the desirable and undesirable forms like:

CMD_DO("APP_PERFORM_TASK") BLAH("APP_PERFORM_TASK")

you'd need something like this in awk (using GNU awk for gensub()):

awk -v s="APP_PERFORM_TASK" 'gensub("CMD_DO\\(\\""s,"","") ~ s' file

i.e. get rid of all of the unwanted occurrences of the string then test whats left.

Comments

0

An awk version

awk '/APP_PERFORM_TASK/ && !/^CMD_DO/' file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.