# Improve grok pattern

**URL:** <https://discuss.elastic.co/t/improve-grok-pattern/246770>\
**Category:** Logstash\
**Created:** [August 28, 2020, 1:26pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770 "2020-08-28T13:26:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![headtea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/headtea/32/24271_2.png) [@headtea](https://discuss.elastic.co/u/headtea)\
**Post date:** [August 28, 2020, 1:26pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770/1 "2020-08-28T13:26:28Z")

</div>

This is some sample data I have:

```
Aug 27 20:21:46 SERVER-150 john: USER=john PWD=/root PID=[9103] CMD="cat /etc/passwd" Exit=[0] CONNECTION=1.2.3.4
Aug 21 13:21:46 SERVER-230 root: USER=root PWD=/etc/folder PID=[9023] CMD="echo test" Exit=[0] CONNECTION=
Aug 24 14:11:46 MACHINE-ESXi-17_23 jack: USER=root PWD=/home/jack PID=[9003] CMD="not a command" Exit=[0] CONNECTION=
Aug 22 12:21:36 LOG_MACHINE-12 root: USER=root PWD=/root PID=[9203] CMD="cat /etc/passwd" Exit=[0] CONNECTION=202.54.12.15

```

This is the pattern I currently have:

```
\A%{SYSLOGTIMESTAMP:sys_timestamp} %{NOTSPACE:Hostname} %{USER:Logged}: USER=%{USER:User} PWD=%{UNIXPATH:Directory} PID=\[%{INT:PID}] CMD=%{QUOTEDSTRING:Command} Exit=\[%{INT:Exit}] CONNECTION=%{GREEDYDATA:Connection}

```

[This is how it looks on kibana](https://i.imgur.com/44EZymx.png).

The problem is that the values for `"Command"` which I highlighted in the image above, are inbetween quotation marks. When someone runs a command with quotation marks in them like `echo "test"` grok fails to parse it. It's also less convenient to read through it.

Is there a way to make grok smarter and have it output the field without quotation marks while also working with "commands" with quotations in them like `echo "test"`? If that's a problem I can also change the sample data but I'm not sure what for. I tried a couple of things and it didn't work.

Huge thanks ahead!

---

<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:** [August 28, 2020, 4:37pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770/2 "2020-08-28T16:37:56Z")

</div>

For the Command field you could try changing CMD=%{QUOTEDSTRING:Command} to CMD="%{DATA:Command}". The Exit= after it will anchor where it stops consuming the message.

---

<div class="post-metadata">

**Author:** ![headtea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/headtea/32/24271_2.png) [@headtea](https://discuss.elastic.co/u/headtea)\
**Post date:** [August 28, 2020, 8:26pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770/3 "2020-08-28T20:26:44Z")

</div>

Thank you so much for the response!

May I ask what the full syntax would be? Would it just be:

```
CMD="%{DATA:Command}" Exit... (keep patten)
```

---

<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:** [August 28, 2020, 8:29pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770/4 "2020-08-28T20:29:14Z")

</div>

> [@headtea](#):
>
> `\A%{SYSLOGTIMESTAMP:sys_timestamp} %{NOTSPACE:Hostname} %{USER:Logged}: USER=%{USER:User} PWD=%{UNIXPATH:Directory} PID=\[%{INT:PID}] CMD="%{DATA:Command}" Exit=\[%{INT:Exit}\] CONNECTION=%{GREEDYDATA:Connection}`

Note that I escaped the ] following the exit code.

---

<div class="post-metadata">

**Author:** ![headtea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/headtea/32/24271_2.png) [@headtea](https://discuss.elastic.co/u/headtea)\
**Post date:** [August 28, 2020, 8:41pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770/5 "2020-08-28T20:41:46Z")

</div>

Yes! Thank you so much!!

May I just ask about the "escape" after the Exit?

---

<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:** [August 28, 2020, 8:47pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770/6 "2020-08-28T20:47:20Z")

</div>

grok is matching a regular expression, and [and] are used to define a character group. So you must escape [ (i.e. use \[) to tell the regexp engine that you want it to match a literal square bracket. I always do the same for the close bracket.

---

<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:** [September 25, 2020, 8:47pm UTC](https://discuss.elastic.co/t/improve-grok-pattern/246770/7 "2020-09-25T20:47:27Z")

</div>

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