# Removing leading character and trailing 2 characters

**URL:** https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634
**Category:** Logstash
**Created:** [January 10, 2020, 4:29pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634 "2020-01-10T16:29:06Z")
**Posts on this page:** 16
**Page:** 1

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 10, 2020, 4:29pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/1 "2020-01-10T16:29:06Z")

</div>

I am using kv to parse my fields due to the fields change depends on the logs that are sent from the siem.

```
            kv {
                            value_split => “=’”
                            field_split => “’ “

```

which work fine but the very first field and the last. due to the extra characters. How do I remove them

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 10, 2020, 4:37pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/2 "2020-01-10T16:37:04Z")

</div>

It did not let me post the log trying again

 ![Capture](https://us1.discourse-cdn.com/elastic/original/3X/0/a/0a3678257282de88830c656889b826cbc167c20c.jpeg)

---

<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: [January 10, 2020, 5:05pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/3 "2020-01-10T17:05:59Z")

</div>

You can use mutate+gsub to trim leading or trailing characters

```
"someField", "^.", ""

```

will remove a leading character

```
"someField", "..$", ""

```

will remove two trailing characters.

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 10, 2020, 5:59pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/4 "2020-01-10T17:59:59Z")

</div>

I am totally a newbie on this. I am not so worried about the entryid field but the device field is required here is my logstash.conf file I commented the gsub lines out after they did not change anything for me.

I am not sure those fields are defined yet and for the devices field what comes out is

Tag IPgoesHERE and the value is /\>

```
input {
        tcp {
                port => 5142
                type => "ossim-events"
                codec => json {
                        charset => "CP1252"
                        }
        }
        syslog {
                type => "syslog"
        }
}

filter {
        mutate {
# gsub => [
# "someField", "^.",""
# "someField", "..$",""
# ]
                add_field => { "Agent_IP" => "%{host}"}
        }

################### ALIENVAULT OSSIM Logs ###########################

if [type] == "ossim-events" {
        kv {
                value_split => "='"
                field_split => "' "
        }
}
}
output {
# stdout { }
        elasticsearch {
                hosts => ["localhost:9200"]

```

Thank you!!

---

<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: [January 10, 2020, 6:41pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/5 "2020-01-10T18:41:03Z")

</div>

After the kv, what is the name of the field that you want to trim?

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 10, 2020, 6:44pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/6 "2020-01-10T18:44:37Z")

</div>

That's the problem the field is defined as the IP of the device. Being the KV splits the field via "' " and the last record ends "'/\>" it messes up.

I did find out the first entry is defined as ID which is fine. So its only the last one that is a problem.

I hopes this helps

![Capture2](https://us1.discourse-cdn.com/elastic/original/3X/e/e/eeee343e81a93d4573fc79b8393f1687a0427647.jpeg)

---

<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: [January 10, 2020, 7:12pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/7 "2020-01-10T19:12:11Z")

</div>

You are not answering the question. What is the name of the field that you want to trim.

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 12, 2020, 10:58pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/8 "2020-01-12T22:58:33Z")

</div>

Device is the last field. I am not sure I understand though, because I was on the understanding that the KV defines all the fields. Being KV does not define device because it does not end as expected that field does not exist.

Thank you

---

<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: [January 12, 2020, 11:31pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/9 "2020-01-12T23:31:07Z")

</div>

Again you are not answering my question.

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 13, 2020, 12:40pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/10 "2020-01-13T12:40:21Z")

</div>

The field changes on every log based on the IP address. The value is /\> I showed you the field in the image that is black. its an IP address.

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 13, 2020, 12:54pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/11 "2020-01-13T12:54:45Z")

</div>

The field is the value that is suppose to be device. so if the last entry is device='10.10.10.10'/\>  
the field would be 10.10.10.10 the value is /\>

I think I know what needs to happen

gsub =\> ["message", "/\>", " /\>"] but that does not seem to work.

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 14, 2020, 7:47pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/12 "2020-01-14T19:47:10Z")

</div>

@Badger

So when I add the

```
mutate {
               gsub => ["message", "/>", ""]

```

logs stop processing and when I go to htop there is a java process that is running at 200+% CPU Normal is in the 60% range

So I thought about it and I did

```
mutate {
               gsub => ["message", "-", "~"]

```

just as a test and it worked completely fine changing all dashes to tilde

here is a complete message unmodified, not with the ~

`<entry id='30330' v='2' fdate='2020-01-14 19:36:16' date='1579030576' plugin_id='1686' sensor='10.2.1.2' src_ip='10.20.5.12' dst_ip='0.0.0.0' src_port='0' dst_port='0' tzone='0.00' datalen='153' data='Jan 14 19:36:16 MAINSERVER-1 Vpxa: info vpxa[2099663] [Originator@6876 sub=vpxLro opID=HB-host-124550@45884-6f9ee44f-1b] [VpxLRO] -- FINISH lro-183201 ' plugin_sid='20' proto='6' ctx='3b5d523a-329d-11e3-98ba-002590d8b8bc' src_host='90d8b8bc-cbce-11e7-9b24-0025991acb2a' dst_host='' src_net='e3c4c510-6b6e-281e-ee4e-12d131ebba47' dst_net='' userdata1='Vpxa' userdata2='2099663' userdata3='FINISH lro' userdata4='Originator@6876' userdata5='vpxLro' userdata6='HB-host-124550@45884-6f9ee44f-1b' userdata7='VpxLRO' idm_host_src='mainserver-1' device='10.20.5.12'/>`

I am not sure when I mess with the \< /\> the gsub does not work

I have also tried

```
grok {
       match => { "message" => "%{IP:device}...$" }}

```

which has not worked but I am guessing my syntax is not correct. This also stops the logging.

Any help would be great

---

<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: [January 14, 2020, 8:17pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/13 "2020-01-14T20:17:46Z")

</div>

> [@DWbank](#):
>

```
mutate { gsub => ["message", "<entry ", "", "message", "/>$", ""] }
kv { source => "message" field_split => " " value_split => "=" trim_key => " " }

```

works fine for me.

```
      "device" => "10.20.5.12",
       "fdate" => "2020-01-14 19:36:16",
      "src_ip" => "10.20.5.12",
   "userdata7" => "VpxLRO",
   "userdata4" => "Originator@6876",
"idm_host_src" => "mainserver-1",
   "userdata2" => "2099663",
   "userdata3" => "FINISH lro",
       "proto" => "6",
        "data" => "Jan 14 19:36:16 MAINSERVER-1 Vpxa: info vpxa[2099663] [Originator@6876 sub=vpxLro opID=HB-host-124550@45884-6f9ee44f-1b] [VpxLRO] -- FINISH lro-183201 ",
   "userdata5" => "vpxLro",
...

```

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 15, 2020, 12:59pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/14 "2020-01-15T12:59:08Z")

</div>

I am beginning to think there is something wrong with my system. This conf works but does not get me what I want.

```
input {
        tcp {
                port => 5142
                type => "ossim-events"
                codec => json {
                        charset => "CP1252"
                        }
        }
}
filter {

################### ALIENVAULT OSSIM Logs ###########################

if [type] == "ossim-events" {
        kv {
                source => "message"
                field_split => "' "
                value_split => "='"
        }

}
}
output {
        elasticsearch {
                hosts => ["localhost:9200"]
                user => username
                password => password
        }
}

```

This does not. Logs completely stop

```
input {
        tcp {
                port => 5142
                type => "ossim-events"
                codec => json {
                        charset => "CP1252"
                        }
        }
}
filter {

mutate {gsub => ["message", "<entry ", "", "message", "/>$", ""] }

################### ALIENVAULT OSSIM Logs ###########################

if [type] == "ossim-events" {
        kv {
                source => "message"
                field_split => " "
                value_split => "="
                trim_key => " "
        }

}
}
output {
        elasticsearch {
                hosts => ["localhost:9200"]
                user => username
                password => password
        }
}

```

If there is anything else I can look at. I am thinking its not conf related.

I added file and path with rubydebug I have a record it does parse correctly.

---

<div class="post-metadata">

### Author: ![DWbank](https://avatars.discourse-cdn.com/v4/letter/d/d6d6ee/32.png) [@DWbank](https://discuss.elastic.co/u/DWbank)
#### Post date: [January 15, 2020, 3:58pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/15 "2020-01-15T15:58:40Z")

</div>

@Badger

Thank you that worked, I had to create a new indices and all new policies, I am not sure if that index was corrupt or what.

---

<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: [February 12, 2020, 3:58pm UTC](https://discuss.elastic.co/t/removing-leading-character-and-trailing-2-characters/214634/16 "2020-02-12T15:58:40Z")

</div>

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