Splunk Search

How to use rex to extract Linux directory sizes and names?

edwinmae
Path Finder

I run a daily script on the server, du -sk, against a certain directory that contains 200 subdirectories and write that to a .txt file

Example output (of the .txt file)

1000 name1 (1000 = size (total) and name1 = subdirectory)
1100 name2
1200 name3


In Splunk this shows in a similar way as above
Time and Event (Event data is 1000 name1) -- example --

Next line = 1100 name2, etc. --- every line looks like a 'separate' event (=line) in Splunk --


Now when I try to Extract new fields it throws:

Error in 'rex' command: Encountered the following error while compiling the regex '(?i)^(?P[^\t]+)': Regex: syntax error in subpattern name (missing terminator) 

Target is to show the output in a graph (likely in MB or GB) per 'subdirectory'. I saw a similar case, but I was not able to re-produce the solution that was stated.

There is very likely a very simple solution for this to separate the e.g. 1000 from the name1, but I have not succeeded myself -- Yeah I know 🙂

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

There's no need for the (?i) flag since your regex does not contain alphas. This command will extract the directory size and name.

... | rex "(?<size>\d+)\s+(?<dir>\w+)" | ...
---
If this reply helps you, Karma would be appreciated.

View solution in original post

0 Karma

edwinmae
Path Finder

Additional question 'to the same scenario': If we would have the following (within the same event)

1000 dir1
1200 dir2
1550 dir3
Etc.


 .... | rex "(?<size>\d+)\s+(?<dir>\w+)" | eval GB=(size/1024)/1024 | timechart mode(GB) as Size by dir

This will give me only the first line, which is 1000 and dir1. How do I extract the sample above so that I have different events for all (directories and total values)?

0 Karma

richgalloway
SplunkTrust
SplunkTrust

There's no need for the (?i) flag since your regex does not contain alphas. This command will extract the directory size and name.

... | rex "(?<size>\d+)\s+(?<dir>\w+)" | ...
---
If this reply helps you, Karma would be appreciated.
0 Karma

edwinmae
Path Finder

Worked -- Thanks!

0 Karma
Get Updates on the Splunk Community!

Splunk Observability for AI

Don’t miss out on an exciting Tech Talk on Splunk Observability for AI!Discover how Splunk’s agentic AI ...

Splunk Enterprise Security 8.x: The Essential Upgrade for Threat Detection, ...

Watch On Demand the Tech Talk on November 6 at 11AM PT, and empower your SOC to reach new heights! Duration: ...

Splunk Observability as Code: From Zero to Dashboard

For the details on what Self-Service Observability and Observability as Code is, we have some awesome content ...