# Get count of lines in a multiline message field

**URL:** <https://discuss.elastic.co/t/get-count-of-lines-in-a-multiline-message-field/265649>\
**Category:** Logstash\
**Created:** [February 26, 2021, 6:36pm UTC](https://discuss.elastic.co/t/get-count-of-lines-in-a-multiline-message-field/265649 "2021-02-26T18:36:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pavani\_Reddy](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Pavani\_Reddy](https://discuss.elastic.co/u/Pavani_Reddy)\
**Post date:** [February 26, 2021, 6:36pm UTC](https://discuss.elastic.co/t/get-count-of-lines-in-a-multiline-message-field/265649/1 "2021-02-26T18:36:59Z")

</div>

Hi Team,

I have a message field with multiline i.e  
Exception in thread "main" java.lang.IllegalStateException: A book has a null property\n\tat com.example.myproject.Author.getBookIds(Author.java:38)  
\n\tat com.example.myproject.Bootstrap.main(Bootstrap.java:14)\nCaused by: java.lang.NullPointerException\n\tat com.example.myproject.Book.getId(Book.java:22)\n\tat com.example.myproject.Author.getBookIds(Author.java:35)

In this message there are 6 lines

I used following ruby code in logstash filter

ruby {  
code =\> 'event.set("numLines", 1 + event.get("message").count("\n"))'  
}

This giving count as '1' not the actual number of lines.

Please, Can anyone help me!

---

<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:** [February 26, 2021, 7:04pm UTC](https://discuss.elastic.co/t/get-count-of-lines-in-a-multiline-message-field/265649/2 "2021-02-26T19:04:40Z")

</div>

See [here](https://discuss.elastic.co/t/count-number-of-line-in-message-field-multiline-message-field/176842/2). You want to count the number of newlines, not the number of occurences of the string backslash-n.

---

<div class="post-metadata">

**Author:** ![Pavani\_Reddy](https://avatars.discourse-cdn.com/v4/letter/p/5f9b8f/32.png) [@Pavani\_Reddy](https://discuss.elastic.co/u/Pavani_Reddy)\
**Post date:** [February 26, 2021, 8:58pm UTC](https://discuss.elastic.co/t/get-count-of-lines-in-a-multiline-message-field/265649/3 "2021-02-26T20:58:16Z")

</div>

Hi @Badger ,

How can get count of number of newlines, Please give some input .

ruby {  
code =\> 'event.set("numLines", 1 +event.get("message").count(" "))'  
}

I tried the above code, with this code I am getting count spaces in a message , not lines.

ruby {  
code =\> 'event.set("numLines", 1 +event.get("message").count("  
"))'  
}

Tried this one, it gives 1 as output value.

Thanks in advance.

---

<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:** [March 26, 2021, 8:58pm UTC](https://discuss.elastic.co/t/get-count-of-lines-in-a-multiline-message-field/265649/4 "2021-03-26T20:58:52Z")

</div>

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