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.


  • filename: string, in the range: "?(/.*)
  • has_header: one of
    • true
    • false
    • yes
    • no
    • on
    • off
  • class_column: int, in the range: 0,99999999999
  • optional_sort_column: int, in the range: 0,99999999999


Prepare CSV:

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,'   >> /tmp/classfiltercsv.csv
echo 'not_defined,Z,net.ipv4.ip_forward,NOT_DEFINED'    >> /tmp/classfiltercsv.csv
echo 'example_class3,1,net.ipv4.ip_forward,'   >> /tmp/classfiltercsv.csv
echo 'also_undefined,0,net.ipv4.ip_forward,NOT_DEFINED'  >> /tmp/classfiltercsv.csv
sed -i 's/$/\r/' /tmp/classfiltercsv.csv


bundle agent example_classfiltercsv

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

      "Filtered data: $(with)" with => string_mustache("", d);
bundle agent __main__


R: Filtered data: [
    "Sort": "1",
    "Token": "net.ipv4.ip_forward",
    "Value": ""
    "Sort": "A",
    "Token": "net.ipv4.ip_forward",
    "Value": "ANYVALUE"
    "Sort": "a",
    "Token": "net.ipv4.ip_forward",
    "Value": ""
    "Sort": "z",
    "Token": "net.ipv4.ip_forward",
    "Value": "ANYVALUE"


  • If the CSV file is stored in a git repository the .gitattributes file can be used to ensure proper line endings.

    For example:

    # .gitattribtues
    *.csv     text eol=crlf
    RFC-4180-non-compliant-line-endings.csv eol=lf
    *.mustache text
    *.sh       text eol=lf

See also: readcsv(), classmatch()


  • Introduced in CFEngine 3.14