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]'
+3
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 notq
,w
,e
,r
or*
. -
[^root]
- Captures any single character that is notr
,o
ort
.
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
.
+6
source to share