A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.cfengine.com/latest/reference-functions-classfiltercsv.html below:

classfiltercsv - CFEngine 3.26 Docs

Prototype: classfiltercsv(filename, has_header, class_column, optional_sort_column)

Return type: data

Description: Parses CSV data from an RFC 4180 compliant file filename (CRLF line endings required), and returns a data variable that is filtered by defined classes. If has_header is set to true, the columns in the first line of the CSV file are used as keys for the data. class_column specifies which column contains class names to filter by.

If optional_sort_column is defined, the data containers will be sorted by the given column. Both class_column and optional_sort_column must be integer indices starting from 0, and must be at most the total amount of columns minus 1.

Arguments:

Example:

Prepare CSV:

code

echo 'ClassExpr,Sort,Token,Value'                        > /tmp/classfiltercsv.csv
echo '# This is a comment'                              >> /tmp/classfiltercsv.csv
echo 'any,A,net.ipv4.ip_forward,ANYVALUE'               >> /tmp/classfiltercsv.csv
echo 'example_class1,z,net.ipv4.ip_forward,ANYVALUE'    >> /tmp/classfiltercsv.csv
echo 'example_class2,a,net.ipv4.ip_forward,127.0.0.3'   >> /tmp/classfiltercsv.csv
echo 'not_defined,Z,net.ipv4.ip_forward,NOT_DEFINED'    >> /tmp/classfiltercsv.csv
echo 'example_class3,1,net.ipv4.ip_forward,127.0.0.4'   >> /tmp/classfiltercsv.csv
echo 'also_undefined,0,net.ipv4.ip_forward,NOT_DEFINED'  >> /tmp/classfiltercsv.csv
sed -i 's/$/\r/' /tmp/classfiltercsv.csv

Policy:

code

bundle agent example_classfiltercsv
{
  classes:
      "example_class1";
      "example_class2";
      "example_class3";

  vars:
      "data_file" string => "/tmp/classfiltercsv.csv";
      "d" data => classfiltercsv($(data_file), "true", 0, 1);

  reports:
      "Filtered data: $(with)" with => string_mustache("", d);
}
bundle agent __main__
{
  methods:
      "example_classfiltercsv";
}

Output:

code

R: Filtered data: [
  {
    "Sort": "1",
    "Token": "net.ipv4.ip_forward",
    "Value": "127.0.0.4"
  },
  {
    "Sort": "A",
    "Token": "net.ipv4.ip_forward",
    "Value": "ANYVALUE"
  },
  {
    "Sort": "a",
    "Token": "net.ipv4.ip_forward",
    "Value": "127.0.0.3"
  },
  {
    "Sort": "z",
    "Token": "net.ipv4.ip_forward",
    "Value": "ANYVALUE"
  }
]

Notes:

See also: data_expand(), readcsv(), classmatch()

History:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4