Splunk Search

Mask the last 4 digits of a number which is 8 digits longer

VipeRafajzat
Explorer

Hello!

I am struggling to mask the last 4 digits of my numbers.

 

| rex field=FIELD_XY mode=sed "s/[0-9#]{3}$/###/g"

 

With this code I am able to mask the last 4 digits of all kind of numbers in my table to ####. So the numbers looking like : 123456####.

What I cannot do is to apply this masking only those numbers which are 8 digits or more long.  Tried several options and played with regex, but it didn't mask it or over masking everything .

Thank you!

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

Try this run-anywhere example query.

| makeresults | eval FIELD_XY="12345~12345678" | eval FIELD_XY=split(FIELD_XY,"~") | mvexpand FIELD_XY
```Above just creates test data```
| rex field=FIELD_XY mode=sed "s/(\d{4,})[0-9#]{4}$/\1####/g"

The regex looks for a group of a least 4 digits followed by 4 digits or octothorpes.  It then retains the group and replaces the remaining four characters with octothorpes.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust
| makeresults | eval _raw="1234567890 12345678 1234567"
| rex mode=sed max_match=0 "s/([0-9]{4})([0-9]{4})($|[^0-9])/\1XXXX\3/g"
0 Karma

VipeRafajzat
Explorer

Thank you!

 

It was almost perfect, except that it failed if the last digit was a #, then it was not masked with XXXX then.

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Try this run-anywhere example query.

| makeresults | eval FIELD_XY="12345~12345678" | eval FIELD_XY=split(FIELD_XY,"~") | mvexpand FIELD_XY
```Above just creates test data```
| rex field=FIELD_XY mode=sed "s/(\d{4,})[0-9#]{4}$/\1####/g"

The regex looks for a group of a least 4 digits followed by 4 digits or octothorpes.  It then retains the group and replaces the remaining four characters with octothorpes.

---
If this reply helps you, Karma would be appreciated.

VipeRafajzat
Explorer

Thank you!

This was the solution what I was looking for 🙂

0 Karma
Get Updates on the Splunk Community!

Observe and Secure All Apps with Splunk

  Join Us for Our Next Tech Talk: Observe and Secure All Apps with SplunkAs organizations continue to innovate ...

Splunk Decoded: Business Transactions vs Business IQ

It’s the morning of Black Friday, and your e-commerce site is handling 10x normal traffic. Orders are flowing, ...

Fastest way to demo Observability

I’ve been having a lot of fun learning about Kubernetes and Observability. I set myself an interesting ...