简体   繁体   中英

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 . Use (ALL|root) instead

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

=                   # 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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