grok command
Note
To see which AWS data source integrations support this PPL command, see Commands.
The grok
command parses a text field with a grok pattern and appends the results to the search result.
Syntax
Use the following syntax:
grok <field> <pattern>
field
-
Mandatory.
-
The field must be a text field.
pattern
-
Mandatory.
-
The grok pattern used to extract new fields from the given text field.
-
If a new field name already exists, it will replace the original field.
Grok pattern
The grok pattern is used to match the text field of each document to extract new fields.
Example 1: Create the new field
This example shows how to create a new field host
for each document.
host
will be the host name after @
in the email
field.
Parsing a null field will return an empty string.
os> source=accounts | grok email '.+@%{HOSTNAME:host}' | fields email, host ; fetched rows / total rows = 4/4 +-------------------------+-------------+ | email | host | |-------------------------+-------------| | jane_doe@example.com | example.com | | arnav_desai@example.net | example.net | | null | | | juan_li@example.org | example.org | +-------------------------+-------------+
Example 2: Override the existing field
This example shows how to override the existing address
field with the street number removed.
os> source=accounts | grok address '%{NUMBER} %{GREEDYDATA:address}' | fields address ; fetched rows / total rows = 4/4 +------------------+ | address | |------------------| | Example Lane | | Any Street | | Main Street | | Example Court | +------------------+
Example 3: Using grok to parse logs
This example shows how to use grok to parse raw logs.
os> source=apache | grok message '%{COMMONAPACHELOG}' | fields COMMONAPACHELOG, timestamp, response, bytes ; fetched rows / total rows = 4/4 +-----------------------------------------------------------------------------------------------------------------------------+----------------------------+------------+---------+ | COMMONAPACHELOG | timestamp | response | bytes | |-----------------------------------------------------------------------------------------------------------------------------+----------------------------+------------+---------| | 177.95.8.74 - upton5450 [28/Sep/2022:10:15:57 -0700] "HEAD /e-business/mindshare HTTP/1.0" 404 19927 | 28/Sep/2022:10:15:57 -0700 | 404 | 19927 | | 127.45.152.6 - pouros8756 [28/Sep/2022:10:15:57 -0700] "GET /architectures/convergence/niches/mindshare HTTP/1.0" 100 28722 | 28/Sep/2022:10:15:57 -0700 | 100 | 28722 | | *************** - - [28/Sep/2022:10:15:57 -0700] "PATCH /strategize/out-of-the-box HTTP/1.0" 401 27439 | 28/Sep/2022:10:15:57 -0700 | 401 | 27439 | | ************** - - [28/Sep/2022:10:15:57 -0700] "POST /users HTTP/1.1" 301 9481 | 28/Sep/2022:10:15:57 -0700 | 301 | 9481 | +-----------------------------------------------------------------------------------------------------------------------------+----------------------------+------------+---------+
Limitations
The grok command has the same limitations as the parse command.