Forum: CFEngine Help
Subject: Re: Matching a "parametric" regex
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,26058,26098#msg-26098

Well, this is a bit embarrassing. As you posted your new message, I realized 
that I never posted my response to this. I was sure I did - I even have the 
test code ready. Where did it go? Don't know.

Anyway, you can do this also by using a selective delete_lines promise, without 
having to read the whole thing in memory. Just delete any lines that contain 
the host name or IP address, but that do not conform to the full-record pattern 
that you want. Here it is:


body common control
{
   inputs => { "cfengine_stdlib.cf" };
   bundlesequence => { "test" };
}
 
bundle agent test
{
  vars:
      "hosts_file" string => "/tmp/hosts";
      "line_count"  int => 
countlinesmatching("^$(sys.ipv4)\s+$(sys.fqhost)\s+$(sys.uqhost).*$", 
"$(hosts_file)");
  files:
      "$(hosts_file)"
        edit_line => ensure_proper_host_entry("$(line_count)");
}

bundle edit_line ensure_proper_host_entry(count)
{
  classes:
      "has_full_record"  expression => isgreaterthan("$(count)", "0");
  delete_lines:
      ".*\b($(sys.ipv4)|$(sys.fqhost)|$(sys.uqhost))\b.*"
        delete_select => not_matching_full_host_record;
  insert_lines:
    !has_full_record::
      "$(sys.ipv4)    $(sys.fqhost) $(sys.uqhost)";
}

body delete_select not_matching_full_host_record
{
      delete_if_not_match_from_list => { 
"^$(sys.ipv4)\s+$(sys.fqhost)\s+$(sys.uqhost).*$" };
}


_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to