<?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: Extracting country codes from phone numbers in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136608#M37390</link>
    <description>&lt;P&gt;yoursearch|eval Onecode=substr(Number,1,2)|  eval Twocode=substr(Number,1,3) |eval Threecode=substr(Number,1,4)| lookup dial_codeCSV  Code as Onecode Outputnew Country| lookup dial_codeCSV  Code as Twocode Outputnew Country|lookup dial_codeCSV  Code as Threecode Outputnew Country| table Number,Twocode,Threecode,Country&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 07:46:45 GMT</pubDate>
    <dc:creator>mjegannathan</dc:creator>
    <dc:date>2020-09-29T07:46:45Z</dc:date>
    <item>
      <title>Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136595#M37377</link>
      <description>&lt;P&gt;Lets see how many of you are up early this Easter Sunday - bonus point on offer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;I have a lookup with a list of phone country codes - copypasta from &lt;A href="http://countrycode.org/"&gt;http://countrycode.org/&lt;/A&gt; - here's an example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Code, Country
+1, USA
+44, UK
...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;My logs are delivered in a nice format, with the phone number parsed as:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Number
+44789xxxxxxx
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The problem I'm having is that country codes are of varying length making a "country code" field extraction difficult.&lt;/P&gt;

&lt;P&gt;Is there anyway, preferably without regex, I can use the intelligence from the CSV to pull out the country code into a separate field?&lt;/P&gt;

&lt;P&gt;Thanks guys!&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 09:22:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136595#M37377</guid>
      <dc:creator>himynamesdave</dc:creator>
      <dc:date>2014-04-20T09:22:40Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136596#M37378</link>
      <description>&lt;P&gt;Hi himynamesdave,&lt;/P&gt;

&lt;P&gt;This is completly untested, but try something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; YourBaseSearch | eval Code="" | eval Country="" | inputcsv YourCSVfile append=true | eval myCountry=if(match(Number, Code), Country, "no match") ...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is still untested &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;UPDATE:&lt;/P&gt;

&lt;P&gt;now this is tested and it is working &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;YourBaseSearchHere | inputcsv YourcsvHere append=t | stats values(Code) AS Code values(Country) AS Country values(Number) AS Number | mvexpand Code | eval Code=substr(Code,2) | eval CountryName=if(match(Number, Code), Country, "No match") | table Number Code CountryName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What is happening here?&lt;BR /&gt;
First you search your events and append some information from the csv, pass the values of the three fields Number Code and Country to stats, expand the multivalued in Code and remove the + from the Code. At last check if the Code matches any number and return a table of Number, International dialing code and Country name.&lt;/P&gt;

&lt;P&gt;Hope this helps to get you started and thx @martin_mueller for pushing me in the right direction...&lt;/P&gt;

&lt;P&gt;Cheers, MuS&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 09:58:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136596#M37378</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-04-20T09:58:45Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136597#M37379</link>
      <description>&lt;P&gt;I don't think that'll work - how does the &lt;CODE&gt;Code&lt;/CODE&gt; and &lt;CODE&gt;Country&lt;/CODE&gt; end up in the event with the &lt;CODE&gt;Number&lt;/CODE&gt; after the appended &lt;CODE&gt;inputcsv&lt;/CODE&gt;?&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 10:07:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136597#M37379</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-04-20T10:07:28Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136598#M37380</link>
      <description>&lt;P&gt;try this...And this is using regex &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;.... your search|rex field=Number "+(?&amp;lt;code&amp;gt;(1|44|91|....)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;For this to work you should have a Number field holding all the phone numbers as you have mentioned example in question.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 10:56:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136598#M37380</guid>
      <dc:creator>krish3</dc:creator>
      <dc:date>2014-04-20T10:56:54Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136599#M37381</link>
      <description>&lt;P&gt;your right, as I said it is untested &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; &lt;BR /&gt;
but see my update .... this could work, maybe ... still untested ...&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 11:26:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136599#M37381</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-04-20T11:26:57Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136600#M37382</link>
      <description>&lt;P&gt;update ping&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 12:07:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136600#M37382</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-04-20T12:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136601#M37383</link>
      <description>&lt;P&gt;That should leave you with one row per country code...?&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 22:13:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136601#M37383</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-04-20T22:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136602#M37384</link>
      <description>&lt;P&gt;How about this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;search yielding Number field
| rex field=Number "^(?&amp;lt;Code_4&amp;gt;(?&amp;lt;Code_3&amp;gt;(?&amp;lt;Code_2(?&amp;lt;Code_1&amp;gt;+\d)\d)\d)\d)"
| lookup country_codes Code as code_4 OUTPUT Country as Country_4
| lookup country_codes Code as code_3 OUTPUT Country as Country_3
| lookup country_codes Code as code_2 OUTPUT Country as Country_2
| lookup country_codes Code as code_1 OUTPUT Country as Country_1
| eval Country = coalesce(Country_4, Country_3, Country_2, Country_1)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've assumed that in case of ambiguous starts to a code (e.g. US +1 vs American Samoa +1684) the longer match should win. I didn't resolve US vs Canada, that's a long list in and of itself.&lt;/P&gt;</description>
      <pubDate>Sun, 20 Apr 2014 22:23:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136602#M37384</guid>
      <dc:creator>martin_mueller</dc:creator>
      <dc:date>2014-04-20T22:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136603#M37385</link>
      <description>&lt;P&gt;Hi himynamesdave,&lt;/P&gt;

&lt;P&gt;If your end goal is to list the country name fora respective phone number, have you tried lookup with country code fields with "*"  &lt;/P&gt;

&lt;P&gt;Have lookups like &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;PhoneNumber Country
+91* India
+1* USA 
.. ..
.. ..
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And try , i guess you dont need to extract country code anymore &lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 07:17:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136603#M37385</guid>
      <dc:creator>adityapavan18</dc:creator>
      <dc:date>2014-04-21T07:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136604#M37386</link>
      <description>&lt;P&gt;Unfortunately this will not work due to the ambiguous starts to a code (e.g. US +1 vs American Samoa +1684) &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Apr 2014 10:59:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136604#M37386</guid>
      <dc:creator>himynamesdave</dc:creator>
      <dc:date>2014-04-21T10:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136605#M37387</link>
      <description>&lt;P&gt;I hope I can hang on too this thread, since my problem is equal:&lt;/P&gt;

&lt;P&gt;phone.csv&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Code, Country
45, Denmark
46, Sweden
47, Norway
48, Poland
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Example line list&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Jun 17 10:27:34 172.30.112.1 AAA: 172.30.34.58 logged in with username 0045696744444

client_ip = 172.30.34.58 
 client_site = House_of_fun 
 eventtype = Portal_User_logged_in Information  
 module = AAA 
 sourcetype = udp:514 
 tag = Information 
 username = 0045696744444 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So then I tried to modify your line and got this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; host="172.30.112.1" username="00*" | inputcsv phone.csv append=t | stats values(Code) AS Code values(Country) AS Country values(username) AS username | mvexpand Code | eval Code=substr(Code,3) | eval CountryName=if(match(username, Code), Country, "No match") | table username Code CountryName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;or this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;host="172.30.112.1" username="00*" | inputcsv C:\Program Files\Splunk\etc\apps\search\lookups\phone.csv append=t | stats values(Code) AS Code values(Country) AS Country values(username) AS username | mvexpand Code | eval Code=substr(Code,3) | eval CountryName=if(match(username, Code), Country, "No match") | table username Code CountryName
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Can you see what is wrong? Why it just give list with all numbers, and on first line I get under Country name "No match"?&lt;/P&gt;

&lt;P&gt;PS, This: "eval Code=substr(Code,3)" should remove the two "00"? (starting from third character)&lt;BR /&gt;
Also tried do add "00" to the csv file, and also with and without the "substr" code&lt;/P&gt;</description>
      <pubDate>Wed, 17 Jun 2015 08:40:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136605#M37387</guid>
      <dc:creator>lakromani</dc:creator>
      <dc:date>2015-06-17T08:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136606#M37388</link>
      <description>&lt;P&gt;Try this:&lt;/P&gt;

&lt;P&gt;host="172.30.112.1" username="00*" &lt;BR /&gt;
| inputcsv phone.csv append=t &lt;BR /&gt;
| stats values(Code) AS Code values(Country) AS Country values(username) AS username &lt;BR /&gt;
| eval username=substr(username,3) &lt;BR /&gt;
| eval CountryName=if(match(username, Code), Country, "No match") &lt;BR /&gt;
| table username Code CountryName&lt;/P&gt;

&lt;P&gt;in your use case the &lt;CODE&gt;username&lt;/CODE&gt; is the phone number so you have to use this in the eval and compare it with the &lt;CODE&gt;Code&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 18 Jun 2015 19:22:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136606#M37388</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2015-06-18T19:22:57Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136607#M37389</link>
      <description>&lt;P&gt;It does work.  When I got some time I will post how.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jun 2015 18:56:56 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136607#M37389</guid>
      <dc:creator>lakromani</dc:creator>
      <dc:date>2015-06-30T18:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting country codes from phone numbers</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136608#M37390</link>
      <description>&lt;P&gt;yoursearch|eval Onecode=substr(Number,1,2)|  eval Twocode=substr(Number,1,3) |eval Threecode=substr(Number,1,4)| lookup dial_codeCSV  Code as Onecode Outputnew Country| lookup dial_codeCSV  Code as Twocode Outputnew Country|lookup dial_codeCSV  Code as Threecode Outputnew Country| table Number,Twocode,Threecode,Country&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:46:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Extracting-country-codes-from-phone-numbers/m-p/136608#M37390</guid>
      <dc:creator>mjegannathan</dc:creator>
      <dc:date>2020-09-29T07:46:45Z</dc:date>
    </item>
  </channel>
</rss>

