简体   繁体   中英

find and replace configuration lines in a file

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.

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!

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM