grep

Table of Contents

Prototype: grep(regex, list)

Return type: slist

Description: Returns the sub-list if items in list matching the anchored regular expression regex.

This function can accept many types of data parameters.

Arguments:

  • regex: regular expression - Regular expression - in the range: .*
  • list: string - CFEngine variable identifier or inline JSON - in the range: .*

Example:

body common control
{
      bundlesequence => { "test" };
}

bundle agent test
{
  vars:

      "mylist" slist => { "One", "Two", "Three", "Four", "Five" };
      "Tlist" slist => grep("T.*","mylist");
      "empty_list" slist => grep("ive","mylist");

      "datalist" data => parsejson('[1,2,3, "Tab", "chive"]');
      "data_Tlist" slist => grep("T.*","datalist");
      "data_empty_list" slist => grep("ive","datalist");

      "todo" slist => { "mylist", "Tlist", "empty_list", "datalist", "data_Tlist", "data_empty_list" };
      "$(todo)_str" string => format("%S", $(todo));

  reports:
      "$(todo): $($(todo)_str)";
}

Output:

R: mylist: { "One", "Two", "Three", "Four", "Five" }
R: Tlist: { "Two", "Three" }
R: empty_list: {  }
R: datalist: [1,2,3,"Tab","chive"]
R: data_Tlist: { "Tab" }
R: data_empty_list: {  }

History: The collecting function behavior was added in 3.9.

See also: About collecting functions, filter(), every(), some(), and none().