I'm aware that this is a really simple question, but I'm struggling to find a solution to this .

I need to automatically find and replace in the /etc/aliases file the following section:

# Person who should get root's mail
#root:   marc

And it needs to look something like:

# Person who should get root's mail
root:  someone@something.tld

And I haven't been able to find a solution. Can you guys jump in with some suggestions? Doesn't need to be sed.


On a GNU system, you can use this:

sed -i '/^#[[:blank:]]Person/{n;s/#root:[[:blank:]]\+marc/root:\tsomeone@something.tld/;}' file

It searches for a line beginning with # Person. Then switches to the next line and replaces #root:<blanks>marc with root:<tab> .... The -i flag edits the file inplace.

-i, \+ and \t are GNU extensions. The standard equivalent of \+ is the wordier \{1,\}. To edit the file in place portably, you'd need to resort to a temporary file. The standard equivalent of \t is to insert a tab character literally.

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.