<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Inputcsv using a regex for the filename? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376409#M110508</link>
    <description>&lt;P&gt;Similar to @Colin Humphreys approach, I created a Python script called "recentfile.py" that takes a directory and returns the name of the most recent file. All you have to do is create the file and then edit commands.conf.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# Takes a directory path (home is var/run/splunk) and returns the most recent filename

import glob
import os
import sys,splunk.Intersplunk

def main(results, settings):
    list_of_files = glob.glob("C:\\Program Files\\Splunk\\var\\run\\splunk\\"+sys.argv[1]+"\\*")
    latest_file = max(list_of_files, key=os.path.getctime)
    results = []
    result = {}
    result['filename'] = latest_file
    results.append(result)
    return results

results, dummyresults, settings = splunk.Intersplunk.getOrganizedResults()
results = main(results, settings)
splunk.Intersplunk.outputResults(results)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 03 Oct 2018 17:05:37 GMT</pubDate>
    <dc:creator>nick405060</dc:creator>
    <dc:date>2018-10-03T17:05:37Z</dc:date>
    <item>
      <title>Inputcsv using a regex for the filename?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376404#M110503</link>
      <description>&lt;P&gt;I'd like to &lt;CODE&gt;inputcsv&lt;/CODE&gt; a file using a wildcard for part of the file name. How can I accomplish this?&lt;/P&gt;

&lt;P&gt;More in-depth description of the problem:&lt;/P&gt;

&lt;P&gt;I have weekly VMware CSVs being externally deposited in var/run/splunk/vcenter_reports, e.g.:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;9-14-18 100057_000759 VM Inventory - Custom Attributes ABCVC.csv
9-07-18 100723_001163 VM Inventory - Custom Attributes ABCVC.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;We just want to ingest these CSVs and reference the latest one, which we've done before for CSVs with static formatting after the date: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;inputcsv [| makeresults | eval filename=strftime(now()-&amp;lt;modifier&amp;gt;,"vcenter_reports\\%Y-%m-%d_rest_of_static_filename.csv") | return $filename]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But, we aren't able to figure out where the dynamic 100057_000759 part of the filename comes from, and therefore don't know how to &lt;CODE&gt;inputcsv&lt;/CODE&gt; the file in Splunk. There are probably a bunch of different ways to do this - how? The easiest would be to &lt;CODE&gt;inputcsv&lt;/CODE&gt; the file using a regex if that's possible in Splunk. Or, we could just open the most recent file in the directory, if that's possible with Splunk. I could alternately schedule a report to run once a week that uses a Python script to rename the file, but that seems excessive.&lt;/P&gt;

&lt;P&gt;Any suggestions would be appreciated.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 17:51:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376404#M110503</guid>
      <dc:creator>nick405060</dc:creator>
      <dc:date>2018-09-17T17:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Inputcsv using a regex for the filename?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376405#M110504</link>
      <description>&lt;P&gt;Is your problem that you don't know how to search only in the latest version of the files that you have ingested? Or are you having issues specifying the &lt;CODE&gt;source&lt;/CODE&gt; properly because they are always different?&lt;/P&gt;

&lt;P&gt;It is possible to get change the &lt;CODE&gt;source&lt;/CODE&gt; for the file to eliminate the parts that are different with each new filename, but I don't know if that is one of your issues, but you do state in your posting "and therefore don't know how to access the file in Splunk."&lt;/P&gt;

&lt;P&gt;Some additional clarification will certainly help.  &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 19:12:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376405#M110504</guid>
      <dc:creator>cpetterborg</dc:creator>
      <dc:date>2018-09-17T19:12:50Z</dc:date>
    </item>
    <item>
      <title>Re: Inputcsv using a regex for the filename?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376406#M110505</link>
      <description>&lt;P&gt;Ah, my problem was that I was trying to use inputcsv ad-hoc in my dashboard to grab the files. Since you can't use wildcards or a regex in inputcsv (as far as I know), I was unable to get files that I needed to wildcard part of the filenames of. &lt;/P&gt;

&lt;P&gt;Instead my approach should have been to set up a new monitored input, and then been able to grab the correct source using wildcards. I'll post my code as an answer in a moment, but will probably not accept my own answer, as there may be people out there who are looking to only use inputcsv and use wildcards.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 19:28:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376406#M110505</guid>
      <dc:creator>nick405060</dc:creator>
      <dc:date>2018-09-17T19:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: Inputcsv using a regex for the filename?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376407#M110506</link>
      <description>&lt;P&gt;@nick405060 - Go ahead and post your answer and accept it.  This solution is the right one for the need.  I don't know of any REST call that allows you to check a directory in an ad hoc manner... &lt;/P&gt;</description>
      <pubDate>Fri, 21 Sep 2018 01:03:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376407#M110506</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2018-09-21T01:03:48Z</dc:date>
    </item>
    <item>
      <title>Re: Inputcsv using a regex for the filename?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376408#M110507</link>
      <description>&lt;P&gt;How about a shell process that autogenerates a lookup table with the csv file names in it.&lt;/P&gt;

&lt;P&gt;The below (adjusted for actual full paths), to list the filepath of the newest csv:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/bin/sh
echo filename &amp;gt; SPLUNKPATH/etc/apps/my_app/lookups/vcenter_reports.csv
ls -t SPLUNKPATH/var/run/splunk/vcenter_reports/*.csv &amp;gt;&amp;gt; SPLUNKPATH/etc/apps/my_app/lookups/vcenter_reports.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This could be executed regularly via cron, yourown custom splunk command, or with something like the command modular input &lt;A href="https://splunkbase.splunk.com/app/1553/#/details"&gt;https://splunkbase.splunk.com/app/1553/#/details&lt;/A&gt; &lt;/P&gt;

&lt;P&gt;Then use your exisitign approach with an iinputlookup:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;inputcsv [| inputlookup  vcenter_reports.csv | return $filename]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Sep 2018 03:36:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376408#M110507</guid>
      <dc:creator>datasearchninja</dc:creator>
      <dc:date>2018-09-21T03:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: Inputcsv using a regex for the filename?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376409#M110508</link>
      <description>&lt;P&gt;Similar to @Colin Humphreys approach, I created a Python script called "recentfile.py" that takes a directory and returns the name of the most recent file. All you have to do is create the file and then edit commands.conf.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;# Takes a directory path (home is var/run/splunk) and returns the most recent filename

import glob
import os
import sys,splunk.Intersplunk

def main(results, settings):
    list_of_files = glob.glob("C:\\Program Files\\Splunk\\var\\run\\splunk\\"+sys.argv[1]+"\\*")
    latest_file = max(list_of_files, key=os.path.getctime)
    results = []
    result = {}
    result['filename'] = latest_file
    results.append(result)
    return results

results, dummyresults, settings = splunk.Intersplunk.getOrganizedResults()
results = main(results, settings)
splunk.Intersplunk.outputResults(results)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Oct 2018 17:05:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/376409#M110508</guid>
      <dc:creator>nick405060</dc:creator>
      <dc:date>2018-10-03T17:05:37Z</dc:date>
    </item>
    <item>
      <title>Re: Inputcsv using a regex for the filename?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/561001#M159450</link>
      <description>&lt;P&gt;Use following search as a saved search for querying a lookup using wildcard characters&lt;BR /&gt;&lt;BR /&gt;| rest /services/data//lookup-table-files&lt;BR /&gt;| fields title&lt;BR /&gt;| where title like "%$param$%"&lt;BR /&gt;&lt;BR /&gt;and provide input as follows&lt;BR /&gt;&lt;BR /&gt;| savedsearch searchName param="alpha beta gamma"&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jul 2021 10:41:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Inputcsv-using-a-regex-for-the-filename/m-p/561001#M159450</guid>
      <dc:creator>grv97</dc:creator>
      <dc:date>2021-07-27T10:41:58Z</dc:date>
    </item>
  </channel>
</rss>

