Bash、Perl 或 Sed,在找到的短语后插入新行
好吧,我想我需要一些可以执行以下操作的东西:
在 /var/lib/asterisk/bin/retrieve_conf
中搜索这行代码:
$engineinfo = engine_getinfo();
紧接着插入这两行:
$engineinfo['engine']="asterisk";
$engineinfo['version']="1.6.2.11";
提前致谢, 乔
Ok I guess I need something that will do the following:
search for this line of code in /var/lib/asterisk/bin/retrieve_conf
:
$engineinfo = engine_getinfo();
insert these two lines immediately following:
$engineinfo['engine']="asterisk";
$engineinfo['version']="1.6.2.11";
Thanks in advance,
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以像这样
一旦确认其有效,
添加
-i
进行修改。它有什么作用以及如何工作?首先,我们告诉 sed 匹配包含字符串的行。然后,我们将在该匹配行上执行
a
命令,即“附加文本”。sed
a
命令的语法为请注意,文字换行符是此语法的一部分。为了使其全部成为一行(并保留复制粘贴功能)来代替文字换行符,我使用
$'\n'
它将告诉 bash 或 zsh 在适当的位置插入真正的换行符。完成这项工作所需的引用有点复杂:您必须退出单引号,以便 bash 可以解释$'\n'
,然后您必须重新输入单引号字符串以防止 bash 解释输入的其余部分。编辑:更新为在一个附加命令中附加两行。
You could do it like this
Add
-i
for modification in place once you confirm that it works.What does it do and how does it work?
First we tell sed to match a line containing your string. On that matched line we then will perform an
a
command, which is "append text".The syntax of a sed
a
command isNote that the literal newlines are part of this syntax. To make it all one line (and preserve copy-paste ability) in place of literal newlines I used
$'\n'
which will tell bash or zsh to insert a real newline in place. The quoting necessary to make this work is a little complex: You have to exit single-quotes so that you can have the$'\n'
be interpreted by bash, then you have to re-enter a single-quoted string to prevent bash from interpreting the rest of your input.EDIT: Updated to append both lines in one append command.
您可以使用 Perl 和
Tie::File
(包含在Perl 发行版):You can use Perl and
Tie::File
(included in the Perl distribution):只是为了对称起见,这里有一个使用 awk 的答案。
Just for the sake of symmetry here's an answer using awk.
您还可以使用
ed
:You may also use
ed
:Perl 一行代码:
A Perl one-liner: