bash unix 脚本中的回车

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8870757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:18:38  来源:igfitidea点击:

Carriage return in unix scripting

bashunix

提问by Houman

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.

我累了,但线车不能正常工作。我什至试过 \r\n 没有运气。

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,

非常感谢,

回答by Mat

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

Unix 文本文件行结束符是\n, ASCII 0x0A(换行/换行)。\r0xOD 是回车。

Replace \rby \nin your sed expression and you should be good to go.

在您的 sed 表达式中替换为\rby \n,您应该很高兴。

回答by Birei

One way with sed(GNU version):

一种方式sed(GNU版本):

Content of script.sed:

内容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>