查找并替换文件中的配置行

发布于 2024-11-29 16:51:19 字数 422 浏览 0 评论 0原文

我正在尝试编写一个 bash 脚本来在配置文件中进行一些替换。

conf 测试文件是这样的:

pm.max_children = 50
bla bla bla pm.max_children
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
;pm.max_requests = 20000

我需要做的替换是:

pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 2000

我需要从 bash 脚本运行这个程序。

I am trying to program a bash script to make some substitutions in a configuration file.

The conf test file is like this:

pm.max_children = 50
bla bla bla pm.max_children
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
;pm.max_requests = 20000

The substitutions I need to make are:

pm.max_children = 5
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 2000

I need to run this program from a bash script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

千寻… 2024-12-06 16:51:19

这是使用 sed 的更简单的版本:

#!/usr/bin/sed -f
s/^\(pm.max_children = \).*/\15/
s/^\(pm.start_servers = \).*/\11/
s/^\(pm.min_spare_servers = \).*/\11/
s/^\(pm.max_spare_servers = \).*/\13/
s/^\(pm.max_requests = \).*/\12000/

您可以将其放入单独的文件中并从脚本中调用它吗?

我同意 Kerrek SB 的观点——如果你能解释一下你想要实现的目标,这将会有所帮助!

Here's a simpler version using sed:

#!/usr/bin/sed -f
s/^\(pm.max_children = \).*/\15/
s/^\(pm.start_servers = \).*/\11/
s/^\(pm.min_spare_servers = \).*/\11/
s/^\(pm.max_spare_servers = \).*/\13/
s/^\(pm.max_requests = \).*/\12000/

Can you put that in a separate file and call it from your script?

I agree with Kerrek SB – it would help if you'd explain what you're trying to accomplish!

格子衫的從容 2024-12-06 16:51:19
#!/usr/bin/perl
use warnings;
use strict;

my %vals = (
    'pm.max_children'      => 5,
    'pm.start_servers'     => 1,
    'pm.min_spare_servers' => 1,
    'pm.max_spare_servers' => 3,
    'pm.max_requests'      => 2000,
);

while (<DATA>) {
    if ( my($param) = /^(\S+)\s*=/ and exists $vals{$1} ) {
        s/\d+$/$vals{$param}/;
    }
    print;
}

__DATA__
pm.max_children= 50
bla bla bla pm.max_children
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 20000
#!/usr/bin/perl
use warnings;
use strict;

my %vals = (
    'pm.max_children'      => 5,
    'pm.start_servers'     => 1,
    'pm.min_spare_servers' => 1,
    'pm.max_spare_servers' => 3,
    'pm.max_requests'      => 2000,
);

while (<DATA>) {
    if ( my($param) = /^(\S+)\s*=/ and exists $vals{$1} ) {
        s/\d+$/$vals{$param}/;
    }
    print;
}

__DATA__
pm.max_children= 50
bla bla bla pm.max_children
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 30
pm.max_requests = 20000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文