Ruby code to generate fields dynamically based on csv

I need dynamically generated fields based on the CSV in the logs.
In this case my csv format can be in any of the following ways:
CSV =
CSV = 0
CSV = 1111;2222;3333;4444444;5555
CSV = 1;2;3;4;5;6;7;8;9;10

So, I want the fields to be created in the following way:
CSV01 = 1
CSV02 = 2
CSV03 = 3
.
.
.

For this I tired the following Ruby code in filter plugin

 ruby {
                        code => '
                                if event.get("CSV") != ""
                                        CSV_Numbers = event.get("CSV").split(";")
                                        for csvNumber in CSV_Numbers
                                                IndexValue = event.get("CSV").index("csvNumber")
                                                fieldName = event.set("CSV0" + IndexValue)
                                                event.set("fieldName", csvNumber)
                                        end
                                end
                              '
                }

But I'm not able to generate fields dynamically. Can someone please help me in this case.

There are several issues with your code. In Ruby names that start with an upper case letter are constants, not variables, so once the first event has set CSV_Numbers it cannot be changed. Similarly for IndexValue. Also, you want fieldName to be a ruby variable, not a field on the event, so do not use event.set.

I would do this using

    ruby {
        code => '
            csv = event.get("CSV")
            if csv != ""
                csvNumbers = csv.split(";")
                csvNumbers.each_index { |x|
                    event.set("CSV0#{x}", csvNumbers[x])
                }
            end

        '
    }

Thanks @Badger !!
That actually helped.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.