Exclude regex bash script

I want to find lastlogin for specific usernames. I want to exclude everything from qwer * and root, but not store anything with user.name

Here's what I already have, but the last part of the regex doesn't work. Any help was appreciated.

lastlog | egrep '[a-zA-Z]\.[a-zA-Z]|[^qwer*][^root]'



source to share

2 answers

This regex doesn't do what you think it does. Let's break it down:

  • [a-zA-Z]

    - symbol [...]

    indicates a class of symbols. You have the means: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w , x, y, z (and capital versions). This captures one character! This is not what you want!
  • \.

    - this is the period. The backslash is required as it .

    means "any character".
  • [a-zA-Z]

    - the same as above.
  • |

    - or sign. Either what happened before, or what later.
  • [^qwer*]

    - Captures any single character that is not q

    , w

    , e

    , r

    or *

  • [^root]

    - Captures any single character that is not r

    , o

    or t


As you can see, this is not exactly what you were doing. Try the following:

lastlog | egrep -v '^(qwer|root$)' | egrep '^[a-zA-Z]+\.[a-zA-Z]+$'


You cannot have a "do not match this group" operator in regular expressions ... This is not regular. Some implementations provide such functionality anyway, namely the PCRE package and Python re




This should do you:

lastlog | egrep -v '^qwer|^root$'


The -v option to grep gives you non-matching lines, not matching ones.

And if you specifically want usernames to be in the form User.Name only, you can add this:

lastlog | egrep -v '^qwer|^root$' | egrep -i '^[a-z]*\.[a-z]*'




All Articles