Splunk Search

format field at search time (phone, ip, MAC, etc..)

mataharry
Communicator

I want to format nicely the fields or events at search time.

by example :
US phone : 11122223333 to (111) 222-3333
ipv4 : 1-2-3-4 to 1.2.3.4
credit card : 43431111222233334444 to 4343-1111-2222-3333-4444

0 Karma
1 Solution

yannK
Splunk Employee
Splunk Employee

You can use the rex command on any field, define macros.
If needed you can also use the SEDCMD to format the events at index time (but this will be definitive)

please replace the field by the one needed, and update the regex based on your needs.


mysearch | eval phone=1112223333 | rex field=phone mode=sed "s/(\d{3})(\d{3})(\d{4})/(\1) \2-\3/g" | table phone

mysearch | eval ip="1-2-133-4" | rex field=ip mode=sed "s/(\d{1,3})-(\d{1,3})-(\d{1,3})-(\d{1,3})/\1.\2.\3.\4/g" | table ip

it can be improved, ip block should be <= 255

mysearch | eval card="43431111222233334444" | rex field=card mode=sed "s/(43\d{2})(\d{4})(\d{4})(\d{4})(\d{4})/\1-\2-\3-\4-\5/g" | table card

this one will only match credit cards starting with 43.

View solution in original post

yannK
Splunk Employee
Splunk Employee

You can use the rex command on any field, define macros.
If needed you can also use the SEDCMD to format the events at index time (but this will be definitive)

please replace the field by the one needed, and update the regex based on your needs.


mysearch | eval phone=1112223333 | rex field=phone mode=sed "s/(\d{3})(\d{3})(\d{4})/(\1) \2-\3/g" | table phone

mysearch | eval ip="1-2-133-4" | rex field=ip mode=sed "s/(\d{1,3})-(\d{1,3})-(\d{1,3})-(\d{1,3})/\1.\2.\3.\4/g" | table ip

it can be improved, ip block should be <= 255

mysearch | eval card="43431111222233334444" | rex field=card mode=sed "s/(43\d{2})(\d{4})(\d{4})(\d{4})(\d{4})/\1-\2-\3-\4-\5/g" | table card

this one will only match credit cards starting with 43.

Get Updates on the Splunk Community!

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...

.conf24 | Learning Tracks for Security, Observability, Platform, and Developers!

.conf24 is taking place at The Venetian in Las Vegas from June 11 - 14. Continue reading to learn about the ...

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...