Is there any way to do a multiline regex check for a file in CFEngine?

There is no function for searching a file for content. The grep() function operates on lists, not files. However using readfile() and regcmp() I was sill able to search for a multi-line string using only native functionaity

In the example below we first create a file with content that spans multiple lines. Then we read that file content into a variable and use a regular expression on the variable content.

 1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637383940
  bundle agent main
        # CFEngine
        # is the agent you're looking for.

        create => "true",
        edit_line => insert_lines( "CFEngine$(const.n)is the agent you're looking for" );


          string => readfile( "/tmp/example.txt" , inf ),
          if => not( isvariable( $(this.promiser) ) );


        # Search for a multi-line string
          expression => regcmp( "CFEngine\Ris\sthe\sagent.*",

          expression => not( regcmp( ".*droid.*",


        "CFEngine $(sys.cf_version)";

        "/tmp/example.txt contains:$(const.n)$(file_content)";

        "I found the agent i'm looking for."
          if => "I_found_the_agent_i_am_looking_for";

        "I see no droids."
          if => "I_did_not_find_droids";

Results in this output:

R: CFEngine 3.11.0
R: /tmp/example.txt contains:
is the agent you're looking for

R: I found the agent i'm looking for.
R: I see no droids.

You may also want to check out this post about building better regular expressions in CFEngine from Neil Watson.