# Compare two fields in logstash

**URL:** <https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506>\
**Category:** Logstash\
**Created:** [August 8, 2016, 5:01pm UTC](https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506 "2016-08-08T17:01:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![niraj\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_kumar/32/4130_2.png) [@niraj\_kumar](https://discuss.elastic.co/u/niraj_kumar)\
**Post date:** [August 8, 2016, 5:01pm UTC](https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506/1 "2016-08-08T17:01:53Z")

</div>

Guys,

Is it possible to compare two fields in logstash and create a new field out of it. I have a AWS S3 logs where the bytes downloaded is sometimes not equal to the actual file downloaded. So what i am trying to find out is the successful downloads based on comparing two fields.

Any help would be great.

--  
Niraj

---

<div class="post-metadata">

**Author:** ![niraj\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_kumar/32/4130_2.png) [@niraj\_kumar](https://discuss.elastic.co/u/niraj_kumar)\
**Post date:** [August 9, 2016, 10:57pm UTC](https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506/2 "2016-08-09T22:57:56Z")

</div>

Okay I got this working today. This is the ruby filter used to complete the task.

```
filter {
    if [type] == "s3-access-log" {
        grok {
            match => { "message" => "%{S3_ACCESS_LOG}" }
        }
        ruby {
            code => "event['successfuldownload'] = event['bytes'] == event['object_size']"
          }
        date {
            locale => "en"
            match => ["timestamp" , "dd/MMM/yyyy:HH:mm:ss Z"]
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 10, 2016, 6:04am UTC](https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506/3 "2016-08-10T06:04:36Z")

</div>

You could also use a Logstash conditional that runs one of two mutate filters that adds the `successfuldownload` field.

---

<div class="post-metadata">

**Author:** ![niraj\_kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_kumar/32/4130_2.png) [@niraj\_kumar](https://discuss.elastic.co/u/niraj_kumar)\
**Post date:** [August 10, 2016, 6:18am UTC](https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506/4 "2016-08-10T06:18:23Z")

</div>

Can you give me a example how? just curious

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 10, 2016, 6:33am UTC](https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506/5 "2016-08-10T06:33:15Z")

</div>

```nohighlight
if [bytes] == [object_size] {
  mutate {
    add_field => {
      "successfuldownload" => true
    }
  }
} else {
  ...
}

```

---

<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:** [July 6, 2017, 4:44am UTC](https://discuss.elastic.co/t/compare-two-fields-in-logstash/57506/6 "2017-07-06T04:44:08Z")

</div>


