# Gsub remove square bracket

**URL:** <https://discuss.elastic.co/t/gsub-remove-square-bracket/226508>\
**Category:** Logstash\
**Created:** [April 4, 2020, 10:44am UTC](https://discuss.elastic.co/t/gsub-remove-square-bracket/226508 "2020-04-04T10:44:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattiacareddu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattiacareddu/32/60917_2.png) [@mattiacareddu](https://discuss.elastic.co/u/mattiacareddu)\
**Post date:** [April 4, 2020, 10:44am UTC](https://discuss.elastic.co/t/gsub-remove-square-bracket/226508/1 "2020-04-04T10:44:54Z")

</div>

I'm trying to remove square bracket from my string using gsub filter.

The string is something like this:  
'["/path/to/1","/path/to/2"]'

I tried different ways to remove the square bracket "[" and "]" in order to get a string like '"/path/to/1","/path/to/2"'.

At first simply tried

```
gsub => [
    "document_images", "[", "",
    "document_images", "]", ""
]

```

Then I tried escaping the "[" with \ and \ but always get an exception at gsub filter.  
I also tried

`gsub => ["document_images", "[\[\]]", ""]`

Again without success.

The same thing works using ruby gsub function:

`'test['.gsub('[', '')`

Any suggestions?

---

<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 4, 2020, 2:20pm UTC](https://discuss.elastic.co/t/gsub-remove-square-bracket/226508/2 "2020-04-04T14:20:11Z")

</div>

```
input { generator { count => 1 lines => ['["/path/to/1","/path/to/2"]' ] } }
filter { mutate { gsub => ["message", "[\[\]]", "" ] } }
output { stdout { codec => rubydebug { metadata => false } } }

```

produces

```
   "message" => "\"/path/to/1\",\"/path/to/2\"",

```

for me.

```
filter { mutate { gsub => ["message", "\[", ""] } }

```

also works. What exception are you getting?

---

<div class="post-metadata">

**Author:** ![mattiacareddu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattiacareddu/32/60917_2.png) [@mattiacareddu](https://discuss.elastic.co/u/mattiacareddu)\
**Post date:** [April 4, 2020, 4:23pm UTC](https://discuss.elastic.co/t/gsub-remove-square-bracket/226508/3 "2020-04-04T16:23:43Z")

</div>

The problem was in the syntax. I was using multiple filters (gsub and split) under the same mutate. Thanks.

---

<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 2, 2020, 4:23pm UTC](https://discuss.elastic.co/t/gsub-remove-square-bracket/226508/4 "2020-05-02T16:23:44Z")

</div>

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