<?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: How to query a CSV file from another CSV file? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417360#M73664</link>
    <description>&lt;P&gt;Why use &lt;CODE&gt;| appendpipe&lt;/CODE&gt;? Why &lt;CODE&gt;| inputcsv&lt;/CODE&gt;? Also, why use a subsearch with and not &lt;CODE&gt;| inputlookup append=t&lt;/CODE&gt;?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Apr 2019 13:12:18 GMT</pubDate>
    <dc:creator>jeffland</dc:creator>
    <dc:date>2019-04-16T13:12:18Z</dc:date>
    <item>
      <title>How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417356#M73660</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;I have 2 CSV file in lookups directory :&lt;BR /&gt;
The first CSV is called "host.csv" and has a field called "host" which corresponds to the hostname&lt;BR /&gt;
The second CSV is called "DCM.csv" and has afield called "Computer Name" which corresponds to the hostname and another field called "Flag"&lt;BR /&gt;
I need to count host by Flag for hosts which exists in the first CSV and in the second CSV&lt;BR /&gt;
Could you help me please?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 11:32:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417356#M73660</guid>
      <dc:creator>jip31</dc:creator>
      <dc:date>2019-04-16T11:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417357#M73661</link>
      <description>&lt;P&gt;What have you tried?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 12:22:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417357#M73661</guid>
      <dc:creator>grittonc</dc:creator>
      <dc:date>2019-04-16T12:22:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417358#M73662</link>
      <description>&lt;P&gt;Just to clarify, I understood this as "There can be hosts which are in only one of the two lookups. I want to count all that appear in the first file by Flag". If you need something else, please clarify.&lt;BR /&gt;
First you'll have to get the lookup file as your search results. This is done with the &lt;CODE&gt;inputlookup&lt;/CODE&gt; command. Since it is a generating command, you need to put a pipe before it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputlookup host.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Run this and you'll see all hosts from your first file. Next, you need to lookup each line of this data in your second file using the correct fields:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| lookup DCM.csv "Computer Name" as host
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;With this command, you have the new field &lt;CODE&gt;Flag&lt;/CODE&gt; in your data for hosts that were in both lookups and which have a value for &lt;CODE&gt;Flag&lt;/CODE&gt; in the second (you'll still have results that are only in the first lookup and also those that are in the first and in the seconds but don't have a value for the field &lt;CODE&gt;Flag&lt;/CODE&gt; in the second, either of these will not have a value in &lt;CODE&gt;Flag&lt;/CODE&gt; however). Now we can count:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| stats count by host Flag
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This will give you a count per host and Flag combination, but hosts without a value in &lt;CODE&gt;Flag&lt;/CODE&gt; will be thrown away. If you want them, you'll need to provide a value before &lt;CODE&gt;stats&lt;/CODE&gt;, for example with &lt;CODE&gt;fillnull&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| fillnull value="no Flag value set" Flag
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Final search might look like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputlookup host.csv
| lookup DCM.csv "Computer Name" as host
| fillnull value="no Flag value set" Flag
| stats count by host Flag
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Apr 2019 12:40:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417358#M73662</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2019-04-16T12:40:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417359#M73663</link>
      <description>&lt;P&gt;Like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|inputcsv host.csv | eval which="host"
| appendpipe [|inputcsv DCM.csv | rename "Computer Name" AS host | eval which="DCM" ]
| stats values(*) AS * dc(which) AS whichCount BY host
| where whichCount=2
| stats count BY Flag
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Apr 2019 12:44:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417359#M73663</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-04-16T12:44:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417360#M73664</link>
      <description>&lt;P&gt;Why use &lt;CODE&gt;| appendpipe&lt;/CODE&gt;? Why &lt;CODE&gt;| inputcsv&lt;/CODE&gt;? Also, why use a subsearch with and not &lt;CODE&gt;| inputlookup append=t&lt;/CODE&gt;?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 13:12:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417360#M73664</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2019-04-16T13:12:18Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417361#M73665</link>
      <description>&lt;P&gt;The user said that he has &lt;CODE&gt;CSV files&lt;/CODE&gt;, which I took very literally.  I agree that &lt;CODE&gt;inputlookup&lt;/CODE&gt; would be better than &lt;CODE&gt;inputcsv&lt;/CODE&gt;.  This kind of a subsearch is unlimited and I have used it for a decade since I invented it to bypass the subsearch-limit, long before &lt;CODE&gt;append=t&lt;/CODE&gt; existed.  Maybe that works just as well but I have no reason to switch now.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 14:18:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417361#M73665</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2019-04-16T14:18:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417362#M73666</link>
      <description>&lt;P&gt;perfect jeffland thanks for your clear answer!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 06:35:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417362#M73666</guid>
      <dc:creator>jip31</dc:creator>
      <dc:date>2019-04-17T06:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417363#M73667</link>
      <description>&lt;P&gt;thanks woodcock your code is perfect and works fine but I have to accept jeffland answer which has answered first&lt;BR /&gt;
have a nice day!!&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 06:36:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417363#M73667</guid>
      <dc:creator>jip31</dc:creator>
      <dc:date>2019-04-17T06:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417364#M73668</link>
      <description>&lt;P&gt;I would argue that having used something for a decade is not a good reason to continue doing it if better options become available. However, I hadn't realized that &lt;CODE&gt;inputcsv&lt;/CODE&gt; also has an &lt;CODE&gt;append=t&lt;/CODE&gt; argument, so it hardly makes a difference in this case. Since the question doesn't say where the csv files are kept (in &lt;CODE&gt;etc/apps/&amp;lt;appname&amp;gt;/lookups&lt;/CODE&gt; or &lt;CODE&gt;/var/run/splunk/csv&lt;/CODE&gt;), either should work depending on lookup location.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 08:13:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417364#M73668</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2019-04-17T08:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to query a CSV file from another CSV file?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417365#M73669</link>
      <description>&lt;P&gt;Thanks. Generally, if you get more than one answer I would recommend choosing the answers which gives a better explanation or links to relevant documentation.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Apr 2019 08:16:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-query-a-CSV-file-from-another-CSV-file/m-p/417365#M73669</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2019-04-17T08:16:05Z</dc:date>
    </item>
  </channel>
</rss>

