17

I have a list containing a set of tags, and would like to exclude one tag, unless another tag exists in the line.

Say I have the following list, and want to exclude B, unless A is present.

[A,B]
[A,C]
[B,C]
[A]
[B]

I can reverse grep for B:

> grep --invert-match "B"
[A,C]
[A]

How can I find the previous list, but also the item containing [A,B]?

top 4 comments
sorted by: hot top controversial new old
[-] Andy@programming.dev 6 points 9 months ago* (last edited 9 months ago)
grep -E '(^[^B]*$|A)'

EDIT: Whoops, I meant to make this a top-level comment.

EDIT 2: On one client it looked like a nested comment and on this other client it looks top level and now I'm a confused old man.

[-] bizdelnick@lemmy.ml 1 points 9 months ago

You don't need parentheses here.

[-] Andy@programming.dev 1 points 9 months ago

~~How dare you?~~

Thanks!

[-] TheLugal@lemmy.world 1 points 9 months ago

Haha, on Lemmy.World it looks like a top level. Thank you, either way :)

this post was submitted on 15 Feb 2024
17 points (100.0% liked)

commandline

1745 readers
1 users here now

founded 1 year ago
MODERATORS