-1
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
user973430
  • 43
  • 5
  • Remove `=*[ALLroot].*/usr/bin/find|` from the regex. – Toto Jun 21 '19 at 11:55
  • I edited the question a bit,,,to make it more clear – user973430 Jun 21 '19 at 12:01
  • It's not more clear. What exactly do you want to remove and why? Because you want only the entries with `ALL` or `root` inside parentheses? – tripleee Jun 21 '19 at 12:33
  • Not exactly. Hes grep is searching for one of the letters A, L, r, o, or t, occuring somewhere in front of /usr/bin/vim. – user1934428 Jun 21 '19 at 12:49
  • Then why are you listing `o` & `L` twice? I think you are either failing to be clear on your intent, failing to understand the consequences of your choices, or very possibly both. Apologies if that sounds combative - sincerely - but if you are here to ask questions, don't deflect answers. Provide *specifics* of why they are inapplicable, and/or consider maybe whether the problem is the underlying paradigm of your design. Maybe what you want is more like `grep -E '\bNOPASSWD:/usr/bin/vim\b'` – Paul Hodges Jun 21 '19 at 13:14

2 Answers2

0

[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
Toto
  • 89,455
  • 62
  • 89
  • 125
0

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.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36