# Using Ruby to count the number of fields in a message, then apply CSV depending on the count?

**URL:** <https://discuss.elastic.co/t/using-ruby-to-count-the-number-of-fields-in-a-message-then-apply-csv-depending-on-the-count/177880>\
**Category:** Logstash\
**Created:** [April 22, 2019, 4:26pm UTC](https://discuss.elastic.co/t/using-ruby-to-count-the-number-of-fields-in-a-message-then-apply-csv-depending-on-the-count/177880 "2019-04-22T16:26:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdberts](https://avatars.discourse-cdn.com/v4/letter/s/e99b99/32.png) [@sdberts](https://discuss.elastic.co/u/sdberts)\
**Post date:** [April 22, 2019, 4:26pm UTC](https://discuss.elastic.co/t/using-ruby-to-count-the-number-of-fields-in-a-message-then-apply-csv-depending-on-the-count/177880/1 "2019-04-22T16:26:14Z")

</div>

I have logs coming from dozens of servers, and two different appliance versions, both of which are in comma-separated format.

In one version, there are 73 fields; in the other, there are 93 fields. I need to be able to count the number of fields in each message, then apply the CSV filter+column names depending on the count. My thinking would be to split the message by commas, then counting the fields.

How would this be accomplished using Ruby? Something like this:

filter {  
ruby {  
#Pseudo code  
code =\> "if count of event.['message'].split(",") == 73: apply these column names;  
else if count of event.['message'].split(",") == 93: apply these column names"  
}  
}

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [April 22, 2019, 4:48pm UTC](https://discuss.elastic.co/t/using-ruby-to-count-the-number-of-fields-in-a-message-then-apply-csv-depending-on-the-count/177880/2 "2019-04-22T16:48:53Z")

</div>

You can count the fields using ruby

```
ruby { code => 'event.set("[@metadata][fields]", 1 + event.get("message").count(","))' }
if [@metadata][fields] == 73 {
     csv { ... }
} else {
     csv { ... }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 20, 2019, 4:49pm UTC](https://discuss.elastic.co/t/using-ruby-to-count-the-number-of-fields-in-a-message-then-apply-csv-depending-on-the-count/177880/3 "2019-05-20T16:49:03Z")

</div>

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