2

I have a file like this

<VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
</VirtualHost>

I would like to achieve this:

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName domain.com
        ServerAlias www.domain.com

        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
</VirtualHost>

I tired this but the line carriage doesn't work properly. I even tried \r\n without luck.

sudo sed -i "s/webmaster@localhost/[email protected]\rServerName domain.com \rServerAlias www.domain.com/" /etc/apache2/sites-available/domain

I get this weird character there between:

<VirtualHost _default_:443>
        ServerAdmin [email protected] ^MServerName domain.com ^MServerAlias www.domain.com

        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
</VirtualHost>

What am I missing?

Many Thanks,

1
  • @Mat: Post that as an answer. Commented Jan 15, 2012 at 15:25

2 Answers 2

3

The Unix text file line ending char is \n, ASCII 0x0A (new line/line feed). \r 0xOD is a carriage return.

Replace \r by \n in your sed expression and you should be good to go.

Sign up to request clarification or add additional context in comments.

Comments

1

One way with sed (GNU version):

Content of script.sed:

## Match line with string 'serveradmin' ignoring case.
/serveradmin/I {
    ## Append text after this line.
    a\  
## Literal text to append until a line not ending with '\'
\tServerName domain.com\
\tServerAlias www.domain.com
}

Run the script:

sed -f script.sed infile

And result:

<VirtualHost *:80>                                                                                                                                                                                                                           
        ServerAdmin webmaster@localhost                                                                                                                                                                                                      
        ServerName domain.com                                                                                                                                                                                                                
        ServerAlias www.domain.com                                                                                                                                                                                                           

        DocumentRoot /var/www/                                                                                                                                                                                                               
        <Directory />                                                                                                                                                                                                                        
                Options FollowSymLinks                                                                                                                                                                                                       
                AllowOverride None                                                                                                                                                                                                           
        </Directory>                                                                                                                                                                                                                         
</VirtualHost>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.