Bash 使用sed插入具有转义搜索模式的行
我将按照的示例在特定的模式匹配位置将其放入文件中。我已经在我的机器上运行了这个问题的示例,它运行得非常好 因此,对于我来说,我特别想插入以下内容:Bash 使用sed插入具有转义搜索模式的行,bash,shell,sed,Bash,Shell,Sed,我将按照的示例在特定的模式匹配位置将其放入文件中。我已经在我的机器上运行了这个问题的示例,它运行得非常好 因此,对于我来说,我特别想插入以下内容: <Location "someplace"> AuthBasicProvider ldap AuthType Basic AuthName "Auth" AuthLDAPURL someurl AuthzLDAPAuthoritative on AuthLDAPBindDN someuse
<Location "someplace">
AuthBasicProvider ldap
AuthType Basic
AuthName "Auth"
AuthLDAPURL someurl
AuthzLDAPAuthoritative on
AuthLDAPBindDN someuser
AuthLDAPBindPassword somepassword
</Location>
AuthBasicProvider ldap
AuthType Basic
AuthName“Auth”
authldapurlsomeurl
奥兹尔达蓬
AuthLDAPBindDN someuser
authldappindpassword somepassword
我想在.conf文件()的末尾插入它,特别是在关闭
之前
然而,当我跑的时候
sed -n 'H;${x;s/\<\/VirtualHost> .*\n/test string &/;p;}' vhost.conf
sed-n'H${x;s/\.*\n/test string&/;p;}'vhost.conf
要进行测试,将打印文件的内容,但不会更改文件。我逃出
是错了还是错过了什么
(我也可以使用其他解决方案将我的内容放入文件。)使用-I开关进行就地替换:
sed -i -n 'H;${x;s/\<\/VirtualHost> .*\n/test string &/;p;}' vhost.conf
sed-i-n'H${x;s/\.*\n/test string&/;p;}'vhost.conf
使用-i开关进行就地更换:
sed -i -n 'H;${x;s/\<\/VirtualHost> .*\n/test string &/;p;}' vhost.conf
sed-i-n'H${x;s/\.*\n/test string&/;p;}'vhost.conf
这在GNU-Sed中起作用:
sed -i -n 'H;${x;s/<\/VirtualHost>.*/ test string\n&/;p;}' vhost.conf
sed-i-n'H${x;s/*/test string\n&/;p;}'vhost.conf
对于OSX/BSD:
sed -i '' -n 'H;${x;s/<\/VirtualHost>.*/ test string\'$'\n''&/;p;}' vhost.conf
sed-i'-n'H${x;s/*/teststring\'$'\n'&/;p;}'vhost.conf
这在GNU-Sed中起作用:
sed -i -n 'H;${x;s/<\/VirtualHost>.*/ test string\n&/;p;}' vhost.conf
sed-i-n'H${x;s/*/test string\n&/;p;}'vhost.conf
对于OSX/BSD:
sed -i '' -n 'H;${x;s/<\/VirtualHost>.*/ test string\'$'\n''&/;p;}' vhost.conf
sed-i'-n'H${x;s/*/teststring\'$'\n'&/;p;}'vhost.conf
这可能适合您(GNU-sed):
cat这可能适合您(GNU-sed):
cat运行该命令后,我在vhost.conf
文件中的任何地方都找不到字符串test string
,这与我上面遇到的问题相同。(此外,运行任一命令显然都会创建另一个文件,vhost.confn
,但据我所知,它与vhost.conf
)是sed-I-n(非-in)。这适用于GNU sed。BSD sed要求您指定一个空白后缀,如-i'',在运行该命令后,我无法在vhost.conf
文件中的任何位置找到字符串test string
,这与我上面遇到的问题相同。(另外,运行这两个命令显然会创建另一个文件,vhost.confn
,但据我所知,它与vhost.conf
)是sed-I-n(非-in)。这与GNU-sed一起工作。BSD-sed需要指定一个空白后缀,如-I'',我需要从中学习很多东西(我正在使用BSD-one)。谢谢!快速跟进问题:相比于测试字符串
,让我的大型多行内容从上面进入这个答案的最佳方法是什么?我可以想出几种方法,但都很难看。我需要从中学习很多东西(我正在使用BSD)。谢谢!快速跟进问题:与测试字符串
相比,将上面的大型多行内容放入此答案的最佳方法是什么?我可以想出几种方法,但都很难看。