# Removing spaces in long log text file using grok

**URL:** <https://discuss.elastic.co/t/removing-spaces-in-long-log-text-file-using-grok/138515>\
**Category:** Logstash\
**Created:** [July 4, 2018, 8:40am UTC](https://discuss.elastic.co/t/removing-spaces-in-long-log-text-file-using-grok/138515 "2018-07-04T08:40:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![serozhka](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@serozhka](https://discuss.elastic.co/u/serozhka)\
**Post date:** [July 4, 2018, 8:40am UTC](https://discuss.elastic.co/t/removing-spaces-in-long-log-text-file-using-grok/138515/1 "2018-07-04T08:40:43Z")

</div>

I have long entry in my text file (just a string), which cannot be controlled, and there are a lot of spaces. So is there a possibility to remove them before pushing to Kafka? I need just simple grok filter with Greedydata.  
So config is:  
input {  
file {  
path =\> "/app/logs/somelong.log"  
type =\> "my\_type\_1"  
tags =\> ["xxx","yyy"]  
}  
}  
filter {  
grok {  
match =\> { "message" =\> "%{GREEDYDATA:LogMessage}"}  
remove\_field =\> ["message"]  
}  
}  
output {  
kafka {  
codec =\> "json"  
bootstrap\_servers =\> "[my.kafka.net:9092](http://my.kafka.net:9092)"  
topic\_id =\> ["SOMETOPIC"]  
}  
}

At this moment, I dont need to divide following string to several fields, just need to trim all spaces in that log file. Unfortunatelly, while pasting it here, all spaces removed, so I am just adding them to represent:

XYZSESSION\<5 spaces here\>b43e0d68-6cd9-4299-8cv 99-cefac7b940a0\<10 spaces here\>b43e0d68-6cd9-4299-8c99-ceyiifac7b940a0\<2 spaces here\>5865za7a68-1a9a-ea38-a520-46dbe35e0784\<3 spaces here\>myserver:5195 2018-07-03T22:00:00.31Z\<20 spaces here\>2018-07-03T22:00:00.31Z

---

<div class="post-metadata">

**Author:** ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)\
**Post date:** [July 4, 2018, 11:04am UTC](https://discuss.elastic.co/t/removing-spaces-in-long-log-text-file-using-grok/138515/2 "2018-07-04T11:04:15Z")

</div>

You can use a Ruby code block to do that for you. Also, since you don't apply any patterns to your log line, grok is not really needed.

There are a couple of ways (whichever looks more readable to you), e.g.

```auto
filter {
    ruby {
        code => "
            event.set('LogMessage', event.get('message').gsub(/ +/, ' '))
            #### or
            event.set('LogMessage', event.get('message').squeeze(' '))
        "
    }
    mutate {
        remove_field => ["message"]
    }
}
```

---

<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:** [August 1, 2018, 11:04am UTC](https://discuss.elastic.co/t/removing-spaces-in-long-log-text-file-using-grok/138515/3 "2018-08-01T11:04:16Z")

</div>

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