简体   繁体   English

模式内的Grep模式

[英]Grep pattern within pattern

cat sudo.txt |tr -d "[:blank:]"|grep '=.*[ALLroot].*/usr/bin/vim'

I want to track below: 我想跟踪以下内容:

=(root)NOPASSWD:/usr/bin/vim
=(ALL)NOPASSWD:/usr/bin/vim
=NOPASSWD:/usr/bin/vim

but not: 但不是:

=(user)NOPASSWD:/usr/bin/find

Given the data presented, 根据给出的数据,

grep -E '\bNOPASSWD:/usr/bin/vim\b' sudo.txt

If that doesn't sufficiently select, please show the examples and explain the missing requirements. 如果选择还不够,请显示示例并解释缺少的要求。

[ALLroot] matches A or L or r or o or t . [ALLroot]匹配ALrot Use (ALL|root) instead 使用(ALL|root)代替

Given: 鉴于:

cat sudo.txt
=(root)NOPASSWD:/usr/bin/vim
=(ALL)NOPASSWD:/usr/bin/vim
=NOPASSWD:/usr/bin/vim
=(user)NOPASSWD:/usr/bin/find

grep -E '=(\((root|ALL)\))?.*/usr/bin/vim' sudo.txt
=(root)NOPASSWD:/usr/bin/vim
=(ALL)NOPASSWD:/usr/bin/vim
=NOPASSWD:/usr/bin/vim

Explanation: 说明:

option -E : extended pattern 选项-E :扩展模式

=                   # equal sign
(                   # start group
  \(                # opening parenthesis
    (root|ALL)      # root OR ALL
  \)                # closing parenthesis
)?                  # end group, optional
.*                  # 0 or more any character
/usr/bin/vim        # literally

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

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