- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to extract file name from path's for both windows and nix format?
att35
Builder
04-17-2023
08:53 AM
Hi,
We have a data source containing File Path's from both Windows and Linux formats. Applying regex separately works but how can I extract all file names regardless of format in a single search?
Following works for all Windows path's but for Linux, entire path gets extracted as file_name
| rex field=file_path "(?P<file_name>[^\\\]+)$"
whereas this one works for Linux but does nothing on the Windows path.
| rex field=file_path ".*\/(?<file_name>.*)$"
Is there a way to use both in same search? or a new regex that would work on either of the two formats?
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

woodcock
Esteemed Legend
04-17-2023
10:58 AM
| rex field=file_path "([^\r\n\s\\\/]+[\\\/])+(?<file_name>.*)$"
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
isoutamo

SplunkTrust
04-17-2023
10:12 AM
Hi
Is this what you are looking for?
| makeresults
| eval file_path="/as/bsb/asda/file.txt|c:\asda\asdadaa\aa\file.txt"
| eval file_path = split(file_path,"|")
| mvexpand file_path
``` Previous prepare sample data ```
| rex field=file_path "(?P<file_name>[^\\/\\\]+)$"
r. Ismo
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
rut
Path Finder
04-17-2023
10:12 AM
| rex field=file_path "(?<file_name>[^\/\\\]+\..+)$"
Negation for / and \, match everything that has at least one dot. Tested on:
C:\test\test.tst
/test/test/test.tst
/test/test/test.tst.tst
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

richgalloway

SplunkTrust
04-17-2023
10:03 AM
This regex works in my limited testing.
| rex field=file_path "(?<file_name>[^\/\\]+)$"
---
If this reply helps you, Karma would be appreciated.
If this reply helps you, Karma would be appreciated.
