Bash、Perl 或 Sed,在找到的短语后插入新行

发布于 2024-11-25 09:15:43 字数 284 浏览 0 评论 0原文

好吧,我想我需要一些可以执行以下操作的东西:

/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

灵芸 2024-12-02 09:15:43

您可以像这样

sed -ne '/$engineinfo = engine_getinfo();/a\'

一旦确认其有效,

添加 -i 进行修改。它有什么作用以及如何工作?

首先,我们告诉 sed 匹配包含字符串的行。然后,我们将在该匹配行上执行 a 命令,即“附加文本”。

sed a 命令的语法为

a\
line of text\
another line
;

请注意,文字换行符是此语法的一部分。为了使其全部成为一行(并保留复制粘贴功能)来代替文字换行符,我使用 $'\n' 它将告诉 bash 或 zsh 在适当的位置插入真正的换行符。完成这项工作所需的引用有点复杂:您必须退出单引号,以便 bash 可以解释 $'\n',然后您必须重新输入单引号字符串以防止 bash 解释输入的其余部分。

编辑:更新为在一个附加命令中附加两行。

\n''$engineinfo['engine']="asterisk";\'

一旦确认其有效,

添加 -i 进行修改。它有什么作用以及如何工作?

首先,我们告诉 sed 匹配包含字符串的行。然后,我们将在该匹配行上执行 a 命令,即“附加文本”。

sed a 命令的语法为


请注意,文字换行符是此语法的一部分。为了使其全部成为一行(并保留复制粘贴功能)来代替文字换行符,我使用 $'\n' 它将告诉 bash 或 zsh 在适当的位置插入真正的换行符。完成这项工作所需的引用有点复杂:您必须退出单引号,以便 bash 可以解释 $'\n',然后您必须重新输入单引号字符串以防止 bash 解释输入的其余部分。

编辑:更新为在一个附加命令中附加两行。

\n''$engineinfo['version']="1.6.2.11";'

一旦确认其有效,

添加 -i 进行修改。它有什么作用以及如何工作?

首先,我们告诉 sed 匹配包含字符串的行。然后,我们将在该匹配行上执行 a 命令,即“附加文本”。

sed a 命令的语法为


请注意,文字换行符是此语法的一部分。为了使其全部成为一行(并保留复制粘贴功能)来代替文字换行符,我使用 $'\n' 它将告诉 bash 或 zsh 在适当的位置插入真正的换行符。完成这项工作所需的引用有点复杂:您必须退出单引号,以便 bash 可以解释 $'\n',然后您必须重新输入单引号字符串以防止 bash 解释输入的其余部分。

编辑:更新为在一个附加命令中附加两行。

\n'';p' /var/lib/asterisk/bin/retrieve_conf

一旦确认其有效,

添加 -i 进行修改。它有什么作用以及如何工作?

首先,我们告诉 sed 匹配包含字符串的行。然后,我们将在该匹配行上执行 a 命令,即“附加文本”。

sed a 命令的语法为

请注意,文字换行符是此语法的一部分。为了使其全部成为一行(并保留复制粘贴功能)来代替文字换行符,我使用 $'\n' 它将告诉 bash 或 zsh 在适当的位置插入真正的换行符。完成这项工作所需的引用有点复杂:您必须退出单引号,以便 bash 可以解释 $'\n',然后您必须重新输入单引号字符串以防止 bash 解释输入的其余部分。

编辑:更新为在一个附加命令中附加两行。

You could do it like this

sed -ne '/$engineinfo = engine_getinfo();/a\'

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 is

a\
line of text\
another line
;

Note 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.

\n''$engineinfo['engine']="asterisk";\'

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 is


Note 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.

\n''$engineinfo['version']="1.6.2.11";'

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 is


Note 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.

\n'';p' /var/lib/asterisk/bin/retrieve_conf

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 is

Note 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.

微暖i 2024-12-02 09:15:43

您可以使用 Perl 和 Tie::File (包含在Perl 发行版):

use Tie::File;
tie my @array, 'Tie::File', "/var/lib/asterisk/bin/retrieve_conf" or die $!; 
for (0..$#array) {
    if ($array[$_] =~ /\$engineinfo = engine_getinfo\(\);/) {
        splice @array, $_+1, 0, q{$engineinfo['engine']="asterisk"; $engineinfo['version']="1.6.2.11";};
        last;
    }   
}

You can use Perl and Tie::File (included in the Perl distribution):

use Tie::File;
tie my @array, 'Tie::File', "/var/lib/asterisk/bin/retrieve_conf" or die $!; 
for (0..$#array) {
    if ($array[$_] =~ /\$engineinfo = engine_getinfo\(\);/) {
        splice @array, $_+1, 0, q{$engineinfo['engine']="asterisk"; $engineinfo['version']="1.6.2.11";};
        last;
    }   
}
流年已逝 2024-12-02 09:15:43

只是为了对称起见,这里有一个使用 awk 的答案。

awk '{ if(/\$engineinfo = engine_getinfo\(\);/) print $0"\n$engineinfo['\''engine'\'']=\"asterisk\";\n$engineinfo['\''version'\'']=\"1.6.2.11\"" ; else  print $0 }' in.txt

Just for the sake of symmetry here's an answer using awk.

awk '{ if(/\$engineinfo = engine_getinfo\(\);/) print $0"\n$engineinfo['\''engine'\'']=\"asterisk\";\n$engineinfo['\''version'\'']=\"1.6.2.11\"" ; else  print $0 }' in.txt
友谊不毕业 2024-12-02 09:15:43

您还可以使用ed

# cf. http://wiki.bash-hackers.org/howto/edit-ed
cat <<-'EOF' | ed -s /var/lib/asterisk/bin/retrieve_conf
H
/\$engineinfo = engine_getinfo();/a
$engineinfo['engine']="asterisk";
$engineinfo['version']="1.6.2.11";
.
wq
EOF

You may also use ed:

# cf. http://wiki.bash-hackers.org/howto/edit-ed
cat <<-'EOF' | ed -s /var/lib/asterisk/bin/retrieve_conf
H
/\$engineinfo = engine_getinfo();/a
$engineinfo['engine']="asterisk";
$engineinfo['version']="1.6.2.11";
.
wq
EOF
柠檬色的秋千 2024-12-02 09:15:43

Perl 一行代码:

perl -pE 's|(\$engineinfo) = engine_getinfo\(\);.*\K|\n${1}['\''engine'\'']="asterisk";\n${1}['\''version'\'']="1.6.2.11";|' file

A Perl one-liner:

perl -pE 's|(\$engineinfo) = engine_getinfo\(\);.*\K|\n${1}['\''engine'\'']="asterisk";\n${1}['\''version'\'']="1.6.2.11";|' file
岁吢 2024-12-02 09:15:43
sed -i  's/$engineinfo = engine_getinfo();/$engineinfo = engine_getinfo();<CTRL V><CNTRL M>$engineinfo['engine']="asterisk"; $engineinfo['version']="1.6.2.11";/' /var/lib/asterisk/bin/retrieve_conf
sed -i  's/$engineinfo = engine_getinfo();/$engineinfo = engine_getinfo();<CTRL V><CNTRL M>$engineinfo['engine']="asterisk"; $engineinfo['version']="1.6.2.11";/' /var/lib/asterisk/bin/retrieve_conf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文