Re: regex quirks
Xander Cage <[email protected]>
| Newsgroups | gmane.comp.sysutils.cfengine.general |
|---|---|
| Message-ID | <[email protected]> |
got it...used the old but gold replace or add stuff...
bundle edit_line itsv_replace_or_add_cron_line ( pattern, line )
# @brief modify lines matching a regular expression eg. ".*\/usr\/bin\/nmon
-F \/var\/log\/nmon.*1.*"
# @param pattern The pattern that should be replaced The pattern must match
the whole line (it is automatically anchored to the start and end of the
line) to
# avoid ambiguity.
# @param The line with which to replace matches of pattern
{
vars:
"cline" string => canonify("$(line)");
"eline" string => escape("$(line)");
replace_patterns:
"^(?!$(eline)$)$(pattern)$"
comment => "replace a pattern",
replace_with => value("$(line)"),
classes => scoped_classes_generic("bundle", "replace_$(cline)");
insert_lines:
"$(line)"
ifvarclass => "replace_$(cline)_reached";
# delete_lines:
#
# "^($(_find))$";
#
# insert_lines:
#
# "$(_replace)";
}
On Friday, August 6, 2021 at 8:23:14 AM UTC+2 Xander Cage wrote:
> when i negate the regex the info message is gone, but no edits are
> done...causality dilemma.
>
> bundle edit_line itsv_modify_cron_line ( _find, _replace )
> # @brief modify lines matching a regular expression eg.
> ".*\/usr\/bin\/nmon -F \/var\/log\/nmon.*1.*"
>
> # @param _find The regular expression that the lines need to match
> # @param _replace String to substitute `_find` with
> {
>
> replace_patterns:
>
> "^(?!$(_find))$"
>
> replace_with => value("$(_replace)");
>
> # delete_lines:
> #
> # "^($(_find))$";
> #
> # insert_lines:
> #
> # "$(_replace)";
>
>
> }
>
>
> On Tuesday, August 3, 2021 at 1:22:49 PM UTC+2 [email protected]
> wrote:
>
>> On Wed, 2021-07-28 at 07:29 -0700, Xander Cage wrote:
>> >
>> > hey,
>> >
>> > i'm toying around in making a bundle for crontab editing (add,
>> > modify, remove) and regex is
>> > beginning to bite me.
>> >
>> > i got an "informational" message that my pattern matching skills are
>> > downlevel.
>> >
>> > /var/cfengine/bin/cf-agent -KI -f ./nmon_cron_test.cf
>> > info: Promised replacement '0 0 * * * /usr/bin/nmon -F
>> > /var/log/nmon/`hostname`_`date +'\%y\%m\%d_\%H\%M'`.nmon -s 300 -c
>> > 288 1>/dev/null 2>/dev/null' for pattern '^.*/usr/bin/nmon -F.*$' is
>> > not properly convergent while editing '/var/spool/cron/crontabs/root'
>> > info: Because the regular expression '^.*/usr/bin/nmon -F.*$'
>> > still matches the end-state replacement string '0 0 * * *
>> > /usr/bin/nmon -F /var/log/nmon/`hostname`_`date
>> > +'\%y\%m\%d_\%H\%M'`.nmon -s 300 -c 288 1>/dev/null 2>/dev/null'
>>
>> The 'info' message explains what the problem is -- the anchor regular
>> expression matches the replacement string. That means the promise
>> doesn't converge -- it will make a change every time it is evaluated.
>> That's generally considered a bad pattern in CFEngine (and any engine
>> based on state specification instead of action specification).
>>
>> You can ignore the message or, ideally, change the regex to only match
>> the undesired state.
>>
>> Thanks.
>>
>> --
>> Vratislav
>>
>
--
You received this message because you are subscribed to the Google Groups "help-cfengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/help-cfengine/b1d06d52-f7bc-4d7a-bb8a-2d850310e7c2n%40googlegroups.com.