V Anon
V Anon

Reputation: 543

How do I grep for lines that have conditions depending on the position of characters?

Say I have a random string of alphabets

and my goal is to match the strings that do not contain the letter 'B' before the appearance of sub-string

'PA'

ATRESFGORPAHJB (Valid)

AHJSFBSDFPAOPQ (Invalid)

So I figured, I would first try to match all those that did not contain letter B before appearance of 'PA'

grep -v '[^ ]*\B' *.txt | {condition for before 'PA'?}

but I could not figure out how to put it into proper format.

What can I do to satisfy the conditions?

Upvotes: 1

Views: 47

Answers (1)

Ed Morton
Ed Morton

Reputation: 203899

grep '^[^B]*PA'

e.g.:

$ cat file
ATRESFGORPAHJB (Valid)
AHJSFBSDFPAOPQ (Invalid)

$ grep -E '^[^B]*PA' file
ATRESFGORPAHJB (Valid)

Upvotes: 1

Related Questions