Here's another guess at what you might want. Uses OPed example input file and hard-coded file name. Warning: Not extensively tested. (This runs under Perl version 5.8.9.)

File edit_nagios_cfg_2.pl:

use warnings; use strict; use autodie; BEGIN { # service(s) to exclude, command(s) to check. # string definitions must be quoted if they may contain whitespace +. my @excluded_services = ('PING', ); my @check_commands = ('check_nrpe', ); # contact(s) to add. # string definitions must be quoted if they may contain whitespace +. use constant AR_ADD_CONTACTS => [ qw(other folks) ]; my ($rx_excluded_services) = map qr{ \b (?: $_) \b }xms, join '|', map quotemeta, reverse sort @excluded_services ; my ($rx_check_commands) = map qr{ \b (?: $_) \b }xms, join '|', map quotemeta, reverse sort @check_commands ; # define constant-like subroutines to include lexicals. sub RX_EXCLUDE_SERVICES () { qr{ service_description [^\n\S]+ $rx_ +excluded_services }xms; } sub RX_CHECK_SERVICES () { qr{ check_command [^\n\S]+ \S* $rx_ch +eck_commands }xms; } sub RX_CONTACT_ENTRY () { qr{ contact_groups [^\n\S]+ \S+ }xms; + } } # end BEGIN open my $fh_in, '<', 'kt.txt'; local $/ = '}'; # character ends of define service { ... } block BLOCK: while (my $block = <$fh_in>) { next BLOCK if $block =~ RX_EXCLUDE_SERVICES; next BLOCK unless $block =~ RX_CHECK_SERVICES; $block = add_contacts($block, RX_CONTACT_ENTRY, AR_ADD_CONTACTS); } continue { print $block; } # end while BLOCK close $fh_in; exit; # subroutines ###################################################### sub add_contacts { my ($block, # block to edit $rx_contact, # regex: contact entry in block $ar_add, # array ref.: contacts to add ) = @_; $block =~ s{ ($rx_contact) } { join ',', $1, @$ar_add }xmse; return $block; }
Output:
c:\@Work\Perl\monks\catalyst_list>perl edit_nagios_cfg_2.pl define service { host_name kr-cont00 service_description PING check_command check_host max_check_attempts 1 check_interval 1 retry_interval 2 check_period 24x7 check_freshness 1 contact_groups admins notification_interval 60 notification_period 24x7 notifications_enabled 1 register 1 } define service { host_name KT service_description Check SSH check_command check_ssh max_check_attempts 1 check_interval 1 retry_interval 2 check_period 24x7 check_freshness 1 contact_groups admins notification_interval 60 notification_period 24x7 notifications_enabled 1 register 1 } define service { host_name KT service_description Controller Load check_command check_nrpe!check_load max_check_attempts 1 check_interval 1 retry_interval 2 check_period 24x7 check_freshness 1 contact_groups admins,other,folks notification_interval 60 notification_period 24x7 notifications_enabled 1 register 1 }


Give a man a fish:  <%-{-{-{-<


In reply to Re: && comparison not working by AnomalousMonk
in thread && comparison not working by catalyst_list

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.