# Need to split comma delimited string where one of the fields contains a backslash

**URL:** <https://discuss.elastic.co/t/need-to-split-comma-delimited-string-where-one-of-the-fields-contains-a-backslash/289962>\
**Category:** Logstash\
**Created:** [November 23, 2021, 3:33pm UTC](https://discuss.elastic.co/t/need-to-split-comma-delimited-string-where-one-of-the-fields-contains-a-backslash/289962 "2021-11-23T15:33:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trwillis](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@trwillis](https://discuss.elastic.co/u/trwillis)\
**Post date:** [November 23, 2021, 3:33pm UTC](https://discuss.elastic.co/t/need-to-split-comma-delimited-string-where-one-of-the-fields-contains-a-backslash/289962/1 "2021-11-23T15:33:14Z")

</div>

I have this message string: "foo,bar,domain\name,blah  
I need to split it into fields so it looks like this:  
field[0]: foo  
field[1]: bar  
field[2]: domain\user  
field[3]: blah

but the split filter splits the message using the backslash "n" first, then splits on the comma so I end up with two messages. The string literally contains "\n" in cases where the user's name starts with the letter "n". I need to first replace the backslash with 2 backslashes, then split on the comma terminator.

---

<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:** [November 23, 2021, 5:04pm UTC](https://discuss.elastic.co/t/need-to-split-comma-delimited-string-where-one-of-the-fields-contains-a-backslash/289962/2 "2021-11-23T17:04:07Z")

</div>

Why do you think you need to transform the backslash?

```
input { generator { count => 1 lines => ['foo,bar,domain\name,blah'] } }
filter { mutate { split => { "message" => "," } } }
output { stdout { codec => rubydebug { metadata => false } } }

```

will produce

```
   "message" => [
    [0] "foo",
    [1] "bar",
    [2] "domain\\name",
    [3] "blah"
],

```

If you really do want to replace one backslash with two you can use

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

```

which will produce

```
  "message" => "foo,bar,domain\\\\name,blah",
```

---

<div class="post-metadata">

**Author:** ![trwillis](https://avatars.discourse-cdn.com/v4/letter/t/9fc348/32.png) [@trwillis](https://discuss.elastic.co/u/trwillis)\
**Post date:** [November 23, 2021, 7:00pm UTC](https://discuss.elastic.co/t/need-to-split-comma-delimited-string-where-one-of-the-fields-contains-a-backslash/289962/3 "2021-11-23T19:00:48Z")

</div>

Thanks, there was something about the way I was sending data to the logstash listener via syslog forwarder. When I send a payload straight to the tcp listener via netcat, it behaves how you said it should. I will explore why it is different coming from my forwarder.

---

<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:** [January 12, 2022, 6:34pm UTC](https://discuss.elastic.co/t/need-to-split-comma-delimited-string-where-one-of-the-fields-contains-a-backslash/289962/5 "2022-01-12T18:34:11Z")

</div>

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