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.

list can be a data container or a regular list.

Arguments:

  • regex: regular expression, in the range: .*
  • list: string, in the range: [a-zA-Z0-9_$(){}\[\].:]+

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: { --empty-list-- }
R: datalist: [1,2,3,"Tab","chive"]
R: data_Tlist: { "Tab" }
R: data_empty_list: { --empty-list-- }