I am trying to use a script to append the host name at the end of a multi-line entry of a specific Host_Alias field in sudoers file.
The current sudoers file has an entry similar to :
Host_Alias srv_linuxestate= host10,host12,host13,host1,host50, host16,host1,host2,host11,host15,host21, host3,host14
My required output would be something like the following where I have added “,host25”
Host_Alias srv_linuxestate= host10,host12,host13,host1,host50, host16,host1,host2,host11,host15,host21, host3,host14,host25
There are more lines before and after this segment.
The values for the hosts could change and is not fixed. Also there could be many more lines as above and these being separated using “” at the end of the incomplete line.
Any help/pointers would be great.
Advertisement
Answer
Here’s a sed
version:
/^Host_Alias/{ # whenever we match Host_Alias at line start : /\$/{N;b} # if backslash, append next line and repeat s/$/,host25/ # add the new host to end of line }
If you need to add your new host to just one of the host aliases, adjust the first match to suit. In your example, it should probably be /^Host_Alias *srv_linuxestate=/
, or /^Host_Alias[ t][ t]*srv_linuxestate=/
if there might be tabs instead of spaces.
If you need it as a one-liner, that becomes sed -i -e '/^Host_Alias[ t][ t]*srv_linuxestate=/{;:;/\$/{N;b};s/$/,host25/;}' /etc/sudoers
.