Unix grep regex containing 'x' but not containing 'y'

前端 未结 6 760
-上瘾入骨i
-上瘾入骨i 2020-12-03 02:09

I need a single-pass regex for unix grep that contains, say alpha, but does not contain beta.

grep \'alpha\' <> | grep -v \'beta\'
6条回答
  •  渐次进展
    2020-12-03 03:02

    I'm pretty sure this isn't possible with true regular expressions. The [^y]*x[^y]* example would match yxy, since the * allows zero or more non-y matches.

    EDIT:

    Actually, this seems to work: ^[^y]*x[^y]*$. It basically means "match any line that starts with zero or more non-y characters, then has an x, then ends with zero or more non-y characters".

提交回复
热议问题