# Replace string in a string field (Ingest Pipeline)

**URL:** <https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [September 14, 2020, 3:34pm UTC](https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553 "2020-09-14T15:34:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BkQc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkqc/32/73699_2.png) [@BkQc](https://discuss.elastic.co/u/BkQc)\
**Post date:** [September 14, 2020, 3:34pm UTC](https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553/1 "2020-09-14T15:34:23Z")

</div>

I have a field for which I would need to replace `&#xD;&#xA;` values by _CrLf_.  
I tried the following but `\r\n` is not recognized (`unexpected character [\"\\r]. The only valid escape sequences in strings starting with [\"] are [\\\\] and [\\\"].`).  
The only way I found to get somewhere similar is by literally creating a new line in my script but even then, it create a `\n` value instead of `\r\n` (or at least it seems according to further manipulations).

```
{
	"script": {
	  "lang": "painless",
	  "source": """
		if(ctx.group?.MyField != null)
		  ctx.group.MyField.replace("&#xD;&#xA;", "\r\n");
		"""
	}
}
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 17, 2020, 1:24pm UTC](https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553/2 "2020-09-17T13:24:36Z")

</div>

the `replace` function returns a value, but does not do an in-place replacement. So you need to assign a variable to the result (likely `ctx.group.MyField = ...`)

---

<div class="post-metadata">

**Author:** ![BkQc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bkqc/32/73699_2.png) [@BkQc](https://discuss.elastic.co/u/BkQc)\
**Post date:** [September 17, 2020, 7:57pm UTC](https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553/3 "2020-09-17T19:57:09Z")

</div>

You are right, I did modify my code in that sens but forgot to change it here. But the problem is still with the string escaping in an ingest painless script.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [September 21, 2020, 9:47am UTC](https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553/4 "2020-09-21T09:47:32Z")

</div>

Do you have a complete [Simulate pipeline API](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/simulate-pipeline-api.html) one could try and reproduce locally?

---

<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:** [October 19, 2020, 9:47am UTC](https://discuss.elastic.co/t/replace-string-in-a-string-field-ingest-pipeline/248553/5 "2020-10-19T09:47:36Z")

</div>

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