I have a bash script for installing a web server on my Ubuntu server.

I'd like, in the /etc/apache2/apache2.conf, to replace

#ServerRoot "/etc/apache2"



#ServerRoot "/etc/apache2"
ServerName localhost


If I do

sed -i 's|#ServerRoot "/etc/apache2"|#ServerRoot "/etc/apache2"\nServerName localhost|' /etc/apache2/apache2.conf

it's working, but I'd like to include the blank line and the # after the #ServerRoot to avoid adding twice ServerName if the script is executed twice.

I tried

sed -i 's|#ServerRoot "/etc/apache2"\n\n#|#ServerRoot "/etc/apache2"\nServerName localhost\n\n#|' /etc/apache2/apache2.conf

But it's not working.


How about

  1. search for #ServerRoot "/etc/apache2"
  2. if found, read and append the next line into sed's pattern space
  3. search the appended pattern space for an empty line after the newline, and substitute ServerName localhost for that

Putting that together,

sed -i '\|#ServerRoot "/etc/apache2"| {N;s|\n$|\nServerName localhost|}' /etc/apache2/apache2.conf

In case the 'empty' line actually contains whitespace, you could modify that to

sed -i '\|#ServerRoot "/etc/apache2"| {N;s|\n\s*$|\nServerName localhost|}' /etc/apache2/apache2.conf
| improve this answer | |
  • Another solution working is the one of muru - see above - but out of context because not using sed. – tweetysat Jul 3 '14 at 11:15
  • What is that ` at the beginning of the sed` command? Is the command the same as sed -i '/regex/ s/the/rest/' filename? – Alaa Ali Sep 5 '14 at 16:01
  • @AlaaAli sorry, I don't see any ` (backtick) character - perhaps it is an artifact of how your browser is rendering the page? – steeldriver Sep 5 '14 at 16:27
  • Oh sorry, something went wrong with my comment. I meant: What is that \ at the beginning of the sed command? Is the command the same as sed -i '/regex/ s/the/rest/' filename? – Alaa Ali Sep 5 '14 at 16:35
  • The leading `\` just tells sed to use the following character as its delimiter - see for example the Addresses section from the GNU sed manual. – steeldriver Sep 5 '14 at 17:16

Finally got the double-\n checking working in sed with help from this. Bit of a mouthful:

sed -Ei ':a;N;$!ba;s/^(#ServerRoot[^\n]*)\n\n/\1\nServerName localhost\n\n/' apache2.conf

And an awk that does the same (longer but easier to understand IMO). The logic and sponge are explained in the last section.

awk '{print}/^#ServerRoot/{getline; if($0=="") print "ServerName localhost"; print}' apache2.conf | sponge apache2.conf

Edit: The sed isn't as hard as I originally thought it was. You just want to match and append (the a command); you don't need to replace. I've tested the following as working:

sed -i '/^#ServerRoot/a ServerName localhost' apache2.conf

You can be more specific with your match if you wish and I'll leave the awk below for posterity's sake.

The editing is fairly trivial with awk but in order to write back into the same file we either need a temporary intermediary file or sponge (from the moreutils package). I'll show both methods.

awk '{print}/^#ServerRoot/{print "ServerName localhost"}' apache2.conf > tmp
mv tmp apache2.conf


awk '{print}/^#ServerRoot/{print "ServerName localhost"}' apache2.conf | sponge apache2.conf

The awk is just printing every line it finds but in the case of a match for ^#ServerName is then writes your ServerName line. I find this sort of processing a lot easier to read than arbitrary "swapping" with sed.

| improve this answer | |
  • Ok, but it's not resolving the problem that if you execute the script twice, the ServerName line will be added twice. – tweetysat Jul 3 '14 at 9:49
  • @tweetysat add a grep check. Better still, use the conf-available folder and create a file. – muru Jul 3 '14 at 9:51
  • @muru I tried your solution creating a file in conf-available. It's working fine. Thanks. – tweetysat Jul 3 '14 at 11:09

Another way (thanks muru) but a little out of context because not using sed :

For Ubuntu 14.04:

Create a file fqdn.conf dans /etc/apache2/conf-available/ containing

ServerName localhost

Create a symbolic link in /etc/apache2/conf-enabled/

sudo ln -s /etc/apache2/conf-available/fqdn.conf /etc/apache2/conf-enabled/fqdn.conf
| improve this answer | |

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.