Movatterモバイル変換


[0]ホーム

URL:


CFEngine documentation homepage

classfiltercsv

Suggest changes
Table of contents

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

Return type:data

Description:Parses CSV data from anRFC 4180 compliant filefilename (CRLF line endings required), and returns a data variable that isfiltered by defined classes. Ifhas_header is set totrue, the columns inthe first line of the CSV file are used as keys for the data.class_columnspecifies which column contains class names to filter by.

Ifoptional_sort_column is defined, the data containers will be sorted by thegiven column. Bothclass_column andoptional_sort_column must be integerindices starting from0, and must be at most the total amount of columnsminus1.

Arguments:

  • filename:string - File name - in the range:"?(/.*)
  • has_header: - CSV file has heading - one of
    • true
    • false
    • yes
    • no
    • on
    • off
  • class_column:int - Column index to filter by, contains classes - in the range:0,99999999999
  • optional_sort_column:int - Column index to sort by - in the range:0,99999999999

Example:

Prepare CSV:

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

Policy:

code
bundleagentexample_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);}bundleagent__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:

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

    For example:

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

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

History:

  • Introduced in CFEngine 3.14

Still need help?

Chat Ask a question on Github Mailing list
Version 
master3.24 (LTS)3.21 (LTS)view all versions

[8]ページ先頭

©2009-2025 Movatter.jp