# Last IP from possible comma separated list of IPs

**URL:** <https://discuss.elastic.co/t/last-ip-from-possible-comma-separated-list-of-ips/231583>\
**Category:** Logstash\
**Created:** [May 7, 2020, 3:56pm UTC](https://discuss.elastic.co/t/last-ip-from-possible-comma-separated-list-of-ips/231583 "2020-05-07T15:56:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![clarkritchie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clarkritchie/32/65755_2.png) [@clarkritchie](https://discuss.elastic.co/u/clarkritchie)\
**Post date:** [May 7, 2020, 3:56pm UTC](https://discuss.elastic.co/t/last-ip-from-possible-comma-separated-list-of-ips/231583/1 "2020-05-07T15:56:07Z")

</div>

I'm struggling to match the last IP in pattern which may contain a comma separated list of IPs.

e.g. both of these appear in my logs:

```auto
foo\t102.35.174.94\tbar

```

- `102.35.174.94` is my desired match

```auto
foo\t102.35.174.94,66.249.83.219\tbar

```

- `66.249.83.219` is my desired match

I haven't seen more than 2 IPs in a row yet, but assume that's possible.

I was hoping something like this would work but no luck yet:

```auto
\t(?<x_forwarded_for>(.*(%{IP}$)))\t

```

Any suggestions? Thank you in advance.

---

<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:** [May 7, 2020, 4:37pm UTC](https://discuss.elastic.co/t/last-ip-from-possible-comma-separated-list-of-ips/231583/2 "2020-05-07T16:37:27Z")

</div>

$ will not work because that anchors to end of line, not end of field. However, the \t effectively anchors to end of field, so removing the $ might work.

---

<div class="post-metadata">

**Author:** ![clarkritchie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/clarkritchie/32/65755_2.png) [@clarkritchie](https://discuss.elastic.co/u/clarkritchie)\
**Post date:** [May 7, 2020, 7:43pm UTC](https://discuss.elastic.co/t/last-ip-from-possible-comma-separated-list-of-ips/231583/3 "2020-05-07T19:43:17Z")

</div>

Thanks for the suggestion.

I believe this works for both single IPs or variable length list of comma separated IPs:

```auto
\t%{GREEDYDATA:x_forwarded_for_temp}\t

```

then:

```auto
mutate {
  split => ["x_forwarded_for_temp" , ","]
  add_field => ["x_forwarded_for", "%{[x_forwarded_for_temp][-1]}" ]
}

```

---

<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:** [June 4, 2020, 7:43pm UTC](https://discuss.elastic.co/t/last-ip-from-possible-comma-separated-list-of-ips/231583/4 "2020-06-04T19:43:25Z")

</div>

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