I have file(sudoers) in which lines can be identical except for the first word in them (different users can execute the same set of commands). I am able to extract such a line using the command:
# grep -v '^ *%' /etc/sudoers |egrep "$users_in_which_I_am_interested | sort|awk '{sub(/^[ \t]+/, "")};1'
tom ALL = NOPASSWD:/usr/bin/pwdadm,/usr/bin/chsec,/usr/bin/chuser
jim ALL = NOPASSWD:/usr/bin/pwdadm,/usr/bin/chsec,/usr/bin/chuser
mark ALL = NOPASSWD:/usr/bin/pwdadm,/usr/bin/chsec,/usr/bin/chuser
peter ALL = NOPASSWD:/usr/bin/pwdadm,/usr/bin/chsec,/usr/bin/chuser
(/etc/sudoers is before that prepared to remove leading and trailing tabs and spaces and replace all multiple whitespaces between words single space). Those lines are identical except for the user. I want to be able to extract lines which have the same command statements configured with the same permissions but different users, and assign the username to a variable and run it through a loop.
# grep -v '^ *%' /etc/sudoers |egrep "$users_in_which_I_am_interested | sort|awk '{sub(/^[ \t]+/, "")};1'| uniq -cf 1
4 tom ALL = NOPASSWD:/usr/bin/pwdadm,/usr/bin/chsec,/usr/bin/chuser
The problem for me comes here - how to find all the lines in the file which contain
"ALL = NOPASSWD:/usr/bin/pwdadm,/usr/bin/chsec,/usr/bin/chuser"
and assign the user in those lines to a variable? "grep"-ing for the string is not easily possible, it is an unknown string and might contain many characters that have to be escaped. ksh, awk and sed can be used (no perl/python and other scr. languages).