Usage of Splunk commands : REX - Splunk on Big Data (2024)

Spread our blog

Usage of Splunk commands: REX

This topic is going to explain to you the Rex command in Splunk with lots of interesting Splunk Rex examples

Usage of Splunk Rex command is as follows :

  • Rex command in splunk is used for field extraction in the search head.
  • This command is used to extract the fields using regular expressions.
  • This command is also used for replacing or substitute characters or digits in the fields by the sed expression.
  • You have to specify any field with it otherwise the regular expression will be applied to the _raw field.
Find below the skeleton of the usage of the Splunk “rex” Command:

rex field=<fieldname> [(regex-expression) ] [ mode=sed <sed-expression>]

Basic syntax of the Splunk rex command

Usage of Splunk commands : REX - Splunk on Big Data (2)

Suppose we have a data which is coming from any of the indexes. Below we have given a sample data.

127.0.0.1 – admin [08/Sep/2018:12:46:37.223 +0530] “GET /en-US/splunkd/__raw/services/search/shelper?output_mode=json&snippet= true&snippetEmbedJS=
false&namespace= search&search=search+index%3D_internal+sourcetype%
3Dsplunkd_ui_access+ %7C+rex+field%3D_raw+%22.*%5Cs%2B%5C%5B
(%3F%3CDATE%3E%5Cd %2B%5C%2F%5Cw%2B%5C%2F%5Cd%2B)
%5C%3A%5Cd%2B.*%22& useTypeahead=true&show
CommandHelp=true&show CommandHistory=true&
showFieldInfo=false&_= 1536390799087 HTTP/1.1″ 200 29647 “-” “
Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/ 68.0.3440.106 Safari
/537.36″ – 96c643368d868c21de48395bc54c65d6 11ms

*************************************************************************************

Splunk Rex Examples 1:

Suppose we want to extract 08/Sep/2018 as DATE.

Query:

index=_internal sourcetype=splunkd_ui_access| rex field=_raw ".*\s+\[(?<DATE>\d+\/\w+\/\d+)\:\d+.*"| table DATE| dedup DATE

Result:

Usage of Splunk commands : REX - Splunk on Big Data (3)

Explanation:

In the above query, we are getting data from the “_internal” index and the sourcetype name is “splunkd_ui_access”. We have extracted the date from the raw log so we have put “field=_raw” with the “rex” command and the new field name is “DATE”. Then by the “table” command, we have taken “DATE” and by the “dedup” command we have removed the duplicate values.

*************************************************************************************

Splunk Rex Examples 2:

Suppose we want to extract 127.0.0.1 as IP.

Query:

index=_internal sourcetype=splunkd_ui_access| rex field=_raw "(?<IP>\d+\.\d+\.\d+\.\d+)\s+.*"| table IP| dedup IP

Result:

Usage of Splunk commands : REX - Splunk on Big Data (4)

Explanation:

In the above query, we are getting data from the “_internal” index and the sourcetype name is “splunkd_ui_access”. We have extracted the ip from the raw log so we have put “field=_raw” with the “rex” command and the new field name is “IP”. Then by the “table” command, we have taken “IP” and by the “dedup” command we have removed the duplicate values.

*************************************************************************************

Splunk Rex Examples 3:

We can extract multiple fields by the Splunk rex command.

Query:

index=_internal sourcetype=splunkd_ui_access| rex field=_raw "(?<IP>\d+\.\d+\.\d+\.\d+)\s+.*\[(?<DATE>\d+\/\w+\/\d+)\:\d+.*"| table DATE,IP| dedup DATE,IP

Result:

Usage of Splunk commands : REX - Splunk on Big Data (5)

Explanation:

In the above query, we are getting data from the “_internal” index and the sourcetype name is “splunkd_ui_access”. We have extracted the IP and date from the raw log so we have put “field=_raw” with the “rex” command and the new field names are “IP” and “DATE”. Then by the “table” command, we have taken the “IP” and “DATE”. At last, by the “dedup “ command we have removed the duplicate values.

*************************************************************************************

Splunk Rex Example 4:

Suppose we want to extract the first two characters from any of the existing fields.

Query:

index=_internal sourcetype=splunkd_ui_access| rex field=method "(?<FIRST_TWO_LETTER>\w\w).*"| table method , FIRST_TWO_LETTER| dedup method , FIRST_TWO_LETTER

Result:

Usage of Splunk commands : REX - Splunk on Big Data (6)

Explanation:

In the above query “method” is an existing field name in “_internal” index and the sourcetype name is “splunkd_ui_access” .

Here we have extracted the first two characters from each of the “method” field values by the “rex” command. So we have given “field=method” for extracting the values from the “method” field and the new field name is “FIRST_TWO_LETTER”. By the “table” command we have taken “method” and “FIRST_TWO_LETTER “. At last by the “dedup” command we have taken the unique values.

*************************************************************************************

You can also know about : Usage OF Stats Function ( [first() , last() ,earliest(), latest()] In Splunk

Splunk Rex Example 5:

By the Splunk rex command we can also replace characters in a field.

Query:

index=_internal sourcetype=splunkd_ui_access| eval CLIENT_IP=clientip| rex field=clientip mode=sed "s/(\d{3})/XXX/g"| table CLIENT_IP,clientip|dedup CLIENT_IP,clientip

Result:

Usage of Splunk commands : REX - Splunk on Big Data (7)

Explanation:

In the above query we are getting data from the “_internal” index and sourcetype name is “splunkd_ui_access”. Here “clientip” is the existing field name and by the “eval” command we have taken the values of “clientip” into the “CLIENT_IP” field.Here we have used “mode=sed”with the “rex” command for replacing the first part of the “clientip” field by “XXX” .For that we have written a sed expression.By the “table” command we have taken the “clientip” and “CLIENT_IP” field.At last by the “dedup” command we have removed the duplicate values.

Now you can effectively utilize Splunk “rex”command in your daily use to meet your requirement !!

Hope you are now comfortable in :Usage of Splunk commands : REX

HAPPY SPLUNKING !!

What’s your Reaction?

+1

+1

4

+1

10

+1

+1

1

+1

+1

Related


Spread our blog

Usage of Splunk commands : REX - Splunk on Big Data (2024)

FAQs

What is the use of the rex command in Splunk? ›

Use the rex command for search-time field extraction or string replacement and character substitution.

How can Splunk help with big data? ›

Splunk's software can be used to examine, monitor, and search for machine-generated big data through a browser-like interface. It makes searching for a particular piece of data quick and easy, and more importantly, does not require a database to store data as it uses indexes for storage.

How to use rex in Splunk dashboard? ›

rex command examples
  1. Use a <sed-expression> to mask values. Use a <sed-expression> to match the regex to a series of numbers and replace the numbers with an anonymized string to preserve privacy. ...
  2. Regular expressions with character classes. In this example, the clientip field contains IP addresses.
Jun 28, 2024

What is the difference between rex and regex in Splunk? ›

yes, you're correct: rex extracts fields, regex searches for a string with rules. If you want to have a statistic for the NewProcessName, you have to extract them and use this new field in the stats command.

What is the usage of Rex? ›

Benefits of Usage
  • Data Extraction: The rex command enables you to extract specific fields from unstructured data, making it easier to analyze and interpret.
  • Flexibility: Regular expressions offer a high degree of flexibility, allowing you to define complex patterns and extract or obfuscate data in a variety of formats.
Mar 20, 2024

Which command function is faster for searches and better for Splunk resources? ›

Use the tstats command. The tstats command performs statistical queries on indexed fields, so it's much faster than searching raw data.

Which Splunk product is used for Hadoop? ›

Splunk Analytics for Hadoop lets you access data in remote Hadoop clusters via virtual indexes and allows you to use the Splunk Search Processing Language to analyze your data using Hadoop and NoSQL data stores.

What big companies use Splunk? ›

Companies Currently Using Splunk Enterprise
Company NameWebsiteSub Level Industry
BAE Systemsbaesystems.comAerospace & Defense
Northrop Grummannorthropgrumman.comAerospace & Defense
The Mitre Corporationmitre.orgAll Other Professional and Technical Services
Lockheed Martinlockheedmartin.comAerospace & Defense
2 more rows

What is the difference between Hadoop and Splunk? ›

Data processing: Both tools are designed to process and analyze data, though they do so in different ways. Hadoop is a batch processing system, while Splunk is designed for real-time processing. Data storage: Both Hadoop and Splunk are capable of storing and managing large amounts of data.

How do I feed data to Splunk? ›

Take a look at the four main ways to get your data into Splunk platform. These include the Universal Forwarder, guided data onboarding in Splunk web, creating data inputs for TCP or UDP traffic, and the HTTP Event Collector (HEC).

How to check raw data in Splunk? ›

Splunk takes the raw data an indexes it, you can then run searches against the data. Via rest, you could run a basic search like "index=*" , which would return the raw events. What aggregates you can calculate will depend on the data you have, and what fields are available.

How to extract data from Splunk dashboard? ›

There are three common ways to extract data from Splunk Infrastructure Monitoring: by using SignalFlow, Splunk's streaming analytics API; by using the /timeserieswindow endpoint in the Splunk API; or from the Splunk UI.

How to extract a field in Splunk? ›

  1. On your add-on homepage, click Extract Fields on the Add-on Builder navigation bar.
  2. On the Extract Fields page, from Sourcetype, select a source type to parse.
  3. From Format, select the data format of the data. Any detected format type is automatically selected and you can change the format type as needed. ...
  4. Click Parse.
Jun 13, 2022

How to use table command in Splunk? ›

To use table, insert the command like any other Splunk command into your SPL, then include the fields in the specific order you want them listed in the table output. Columns are displayed in the order specified in the SPL and each row represents an event.

What is spath in Splunk? ›

What is the Splunk spath Command? The spath command extracts fields and their values from either XML or JSON data. You can specify location paths or allow spath to run in its native form.

What is the use of Find command? ›

Here are some examples demonstrating the usage of the find command:
  • Search for a File by Name: find /path/to/search -name "filename” ...
  • Search for Files Based on Size: find /path/to/search -size +1M. ...
  • Search for Files Modified Within a Time Range: find /path/to/search -mtime -7 -mtime +3.
Dec 11, 2023

What are the Splunk commands? ›

Some of the common distributable streaming commands are: eval, fields, makemv, rename, regex, replace, strcat, typer, and where.

What is the use of eval command? ›

Use the eval command to calculate the value of an expression and display the value in a new field. Note: While the stats command calculates statistics based on existing fields, the eval command creates new fields by using existing fields and arbitrary expressions.

What is the use of lookup command in Splunk? ›

The SPL2 lookup command enriches your source data with related information that is in a lookup dataset. Field-value pairs in your source data are matched with field-value pairs in a lookup dataset. You can either append to or replace the values in the source data with the values in the lookup dataset.

Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6222

Rating: 5 / 5 (80 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.