Houman
Houman

Reputation: 66320

Carriage return in unix scripting

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,

Upvotes: 2

Views: 8191

Answers (2)

Mat
Mat

Reputation: 206659

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.

Upvotes: 3

Birei
Birei

Reputation: 36252

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>

Upvotes: 1

Related Questions