0

My /etc/aliases file needs editing but since I have the same file on over 100 hosts I want to just script it and put it to work. I have several alias entries and in one of them I want to remove the user along with the comma separator, like this:

alias_name1:    name1@example.com,name2@example.com,name3@example.com
alias_name2:    name1@example.com,name2@example.com,name3@example.com

So if I want to remove just ",name2@example.com" in "alias_name1" but not "alias_name2" how can I do that as the only example I have now is either remove the string from the entire file or remove the string from a specific line # in the file and since some of the files have a different number of lines that won't work either.

  • Have you considered using revision control? Have you considered augtool (augeas)? The 2nd can be used to edit config files. – ctrl-alt-delor Jul 6 '18 at 17:13
1

to remove just ",name2@example.com" in "alias_name1" but not "alias_name2"

sed approach:

sed -E '/^alias_name1/ s/,name2@example\.com(,?)/\1/' /etc/aliases

The trick with the 1st captured group (...) will also cover cases when ,name2@example.com figures at the end of the line or in the middle.


The output:

alias_name1:    name1@example.com,name3@example.com
alias_name2:    name1@example.com,name2@example.com,name3@example.com
0

You can preface a sed subsitution command with a search parameter:

sed '/search_string/ s/remove_string//'

The above command will operate only on lines that match the /search_string/ regexp, substituting a null string for what was the remove_string match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.