[colug-432] Regex
Steve Roggenkamp
steveroggenkamp at pobox.com
Sun May 17 23:31:45 EDT 2015
This worked for me:
sed -e 's:^\(Defaults.*env_reset\) *$:\1\
Defaults editor=/usr/bin/nano:' /etc/sudoers > tmp
if [ -s tmp ]; then mv sudoers sudoers.bak; mv tmp sudoers; fi
sudoers before:
...
Defaults env_reset
Defaults mail_badpass
Defaults
secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
...
sudoers after:
...
Defaults env_reset
Defaults editor=/usr/bin/nano
Defaults mail_badpass
Defaults
secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
...
In sed use '\(' and '\)' to delimit the text you want to save from a
regular expression and \n where 1<=n<=9 to indicate the saved text from
the regular expression. Sed uses regular expressions from the Bell Labs
formulation, not Perl. The backslash at the end of the first line
indicates sed is to swallow the newline character as a part of the
replacement expression, otherwise it will complain about a
non-terminated expression.
Using the '-e' option allows you to have multiple editing expressions in
a single go. I use it even when I have just one expression to do out of
habit.
I don't think I have ever used the -i option. Years ago I blew away
several months of work using sed to perform an edit on a number of
source code files. Then I found out the sysadmin could not restore my
code from backup. Ever since I've been leery of doing something that
could not be restored. I guess I'm from the old school.
Hope this helps.
Steve
On 05/17/2015 02:03 AM, Steve VanSlyck wrote:
> So I have a copy of sudoers in my home directory for purposes of messing
> it up (or not).
>
> I want to locate the line
>
> Defaults Env_reset [unknown number of spaces in middle, zero to unknown
> number of spaces at end of line]
>
> and after that line insert a line that says
>
> Defaults editor=/bin/nano [four spaces in middle, no spaces at
> end of line]
>
> I think $1 copies the prior group, which is identified by parens. So
> what happens is that sed finds the original line, then replaces that
> line with the original line plus a newline plus an additional line to
> set the editor.
>
> Does this look like a reasonably good sed/RegEx line to do that? (Point
> was noted about not using i.bak yet, but this is not working on the
> actual sudoers file.)
>
> sed -i.bak 's:(^Defaults\s*Env_reset\s*)$:$1\nDefaults
> editor=/bin/nano:' ./sudoers
>
>
>
> _______________________________________________
> colug-432 mailing list
> colug-432 at colug.net
> http://lists.colug.net/mailman/listinfo/colug-432
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.colug.net/pipermail/colug-432/attachments/20150518/d067a3f3/attachment.html
More information about the colug-432
mailing list