简体   繁体   English

排除正则表达式bash脚本

[英]Exclude regex bash script

I want to find the lastlogin on certain usernames. 我想在某些用户名上找到lastlogin。 I want to exclude anything starting with qwer* and root but keep anything with user.name 我想排除以qwer *和root开头的任何内容,但保留user.name的任何内容

Here is what I have already, but the last part of the regex doesn't work. 这是我已经拥有的,但正则表达式的最后一部分不起作用。 Any help appreciated. 任何帮助赞赏。

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

That regexp doesn't do what you think it does. 该正则表达式不符合您的想法。 Lets break it down: 让我们分解一下:

  • [a-zA-Z] - the [...] denotes a character class. [a-zA-Z] - [...]表示一个字符类。 What you have 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 the capital versions). 你有什么意思: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(和资本版本)。 This captures a single character ! 这捕获了一个角色 That's not what you want! 那不是你想要的!
  • \\. - this is a period. - 这是一个时期。 Needs the backslash since . 从那以后需要反斜杠. means "any character". 意思是“任何角色”。
  • [a-zA-Z] - same as above. [a-zA-Z] - 与上述相同。
  • | - or sign. - 或签字。 Either what came before, or what comes afterwards. 无论是之前发生的事情,还是之后发生的事情。
  • [^qwer*] - Captures any single character that's not q , w , e , r , or * . [^qwer*] - 捕获任何不是 qwer*单个字符。
  • [^root] - Captures any single character that's not r , o , or t . [^root] - 捕获任何不是 rot单个字符。

As you can see, that's not exactly what you were going for. 正如您所看到的,这并不完全是您想要的。 Try the following: 请尝试以下方法:

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

You can't have "don't match this group" operator in regexps... That's not regular. regexps中不能有“不匹配此组”运算符...这不是常规的。 Some implementations do provide such functionality anyway, namely PCRE and Python's re package. 一些实现确实提供了这样的功能,即PCRE和Python的re打包。

This should do you: 这应该是你:

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

The -v option to grep gives you the non-matching lines rather than the matching ones. grep的-v选项为您提供不匹配的行而不是匹配的行。

And if you specifically want user names only of the form User.Name then you can add this: 如果您特别想要User.Name形式的用户名,那么您可以添加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM