httpd.conf中的bash sed替换
我正在尝试使用sed和bash替换文件中的一行文本。这是我的密码。我想我的结缔组织可能已经关闭了 sed-e's/DocumentRoot/var/www/html/s///usr/share/rt3/html/'/etc/httpd/conf/httpd.confhttpd.conf中的bash sed替换,bash,text,sed,substitution,Bash,Text,Sed,Substitution,我正在尝试使用sed和bash替换文件中的一行文本。这是我的密码。我想我的结缔组织可能已经关闭了 sed-e's/DocumentRoot/var/www/html/s///usr/share/rt3/html/'/etc/httpd/conf/httpd.conf 使用sed和文件路径时,更容易避免使用“/”分隔符,否则每次都会转义它。这种“纠察篱笆”格式使阅读变得很糟糕 假设您试图将“DocumentRoot/var/www/html”替换为“DocumentRoot/usr/share/r
使用sed和文件路径时,更容易避免使用“/”分隔符,否则每次都会转义它。这种“纠察篱笆”格式使阅读变得很糟糕 假设您试图将“DocumentRoot/var/www/html”替换为“DocumentRoot/usr/share/rt3/html/”,请尝试以下操作:
sed 's_DocumentRoot /var/www/html_DocumentRoot /usr/share/rt3/html/_' /etc/httpd/conf/httpd.conf
例如:
~> echo "DocumentRoot /var/www/html" | sed 's_DocumentRoot /var/www/html_DocumentRoot /usr/share/rt3/html/_'
DocumentRoot /usr/share/rt3/html/
~>
编辑:从您的评论中,您希望1)打开文件,2)替换字符串,3)保存对原始文件的更改。在这种情况下,可以使用sed的-i
标志(就地编辑)。试试这个:
sed -i 's_DocumentRoot /var/www/html_DocumentRoot /usr/share/rt3/html/_' /etc/httpd/conf/httpd.conf
例如:
~> echo "DocumentRoot /var/www/html" > file
~> cat file
DocumentRoot /var/www/html
~> sed -i 's_DocumentRoot /var/www/html_DocumentRoot /usr/share/rt3/html/_' file
~> cat file
DocumentRoot /usr/share/rt3/html/
这不起作用,但你确实明白我要做什么。这就是我试图编写的脚本#打开/etc/httpd/conf/httpd.conf Change DocumentRoot“/var/www/html”To DocumentRoot“/usr/share/rt3/html”#要打开文件、进行更改并重新写入同一文件,最好使用sed的
-i
标志(就地编辑)。尝试以下操作:sed-i's_DocumentRoot/var/www/html_DocumentRoot/usr/share/rt3/html/'/etc/httpd/conf/httpd.conf
使用sed-i's_DocumentRoot/var/www/html_DocumentRoot/usr/share/rt3/html/'/etc/httpd/conf/httpd.conf仍然没有更改文件中是否确实存在目标字符串?具体来说,DocumentRoot/var/www/html
(包括空格)。我在原始文章中添加了一个示例,以便演示如何使用它。