# Logstash mutate field

**URL:** <https://discuss.elastic.co/t/logstash-mutate-field/280097>\
**Category:** Logstash\
**Created:** [July 30, 2021, 7:03pm UTC](https://discuss.elastic.co/t/logstash-mutate-field/280097 "2021-07-30T19:03:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hamza\_El\_Aouane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hamza_el_aouane/32/82411_2.png) [@Hamza\_El\_Aouane](https://discuss.elastic.co/u/Hamza_El_Aouane)\
**Post date:** [July 30, 2021, 7:03pm UTC](https://discuss.elastic.co/t/logstash-mutate-field/280097/1 "2021-07-30T19:03:42Z")

</div>

Hi everyone,  
I hope somebody can help me with this issue I am facing with logstash.

I am receiving some syslog, and trying to put in place some filtering and merging together 2 fields.

the simple structure that I want to implement is as follow:

I have 2 fields:

```auto
destinationUserName. sourceUsername
user1
                                                  user2

```

what I want is to merge together those 2 fields in one, which I called `userID`

```auto
filter {
    mutate {
        add_field => {"userID" => "%{destinationUserName}%{sourceUserName}"}
  }
}

}

```

this worked just fine, until I realised there was a problem.

the problem is, if both fields have data, it does the merging correctly, like this

```auto
input:
destinationUserName. sourceUsername
user1. user2

output:

userId
user1 user2
                                                  

```

but if one one of the fields is empty, I get this output

```auto
destinationUserName. sourceUsername
user1

output:
userID
user1%{sourceUsername}

```

which is not something I want to see in my logs.

Please guys, can somebody help to understand how can I set my logstash to merge and show only the field that has data in it?

this is the output I am expecting

```auto
input
destinationUserName. sourceUsername
user1
                                                  user2
output:
userID
user1
user2

```

thank you so much for your help

---

<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:** [July 30, 2021, 7:56pm UTC](https://discuss.elastic.co/t/logstash-mutate-field/280097/2 "2021-07-30T19:56:27Z")

</div>

You could do

```
if [destinationUserName] and [sourceUsername] {
    mutate { add_field => { "userID" => "%{destinationUserName}%{sourceUserName}" } }
else if [destinationUserName] {
    mutate { add_field => { "userID" => "%{destinationUserName}" } }
} else if [sourceUsername] {
    mutate { add_field => { "userID" => "%{sourceUserName}" } }
}

```

Alternatively

```
mutate { gsub => ["userID", "%{[^}]+}", "" ] }
```

---

<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 27, 2021, 7:56pm UTC](https://discuss.elastic.co/t/logstash-mutate-field/280097/3 "2021-08-27T19:56:27Z")

</div>

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