<?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: is there a cleaner way to zeropad numeric values, ie add leading zeros? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37814#M8547</link>
    <description>&lt;P&gt;Using &lt;CODE&gt;printf&lt;/CODE&gt; conversion function this is possible using something like &lt;CODE&gt;printf("%04d",fieldName)&lt;/CODE&gt;. Refer to documentation: &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/ConversionFunctions#Flag_characters"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/ConversionFunctions#Flag_characters&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Following is a run anywhere example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| fields - _time
| eval data=1
| eval padded_data= printf("%04d",data)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Jun 2019 13:50:45 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2019-06-27T13:50:45Z</dc:date>
    <item>
      <title>is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37800#M8533</link>
      <description>&lt;P&gt;I have a macro that implements a conversion algorithm.   At one point in that algorithm I have to add leading zeros to make sure that a hex value has 8 digits, and not less.   &lt;/P&gt;

&lt;P&gt;What I've done is a little clunky, and it only zeropads from 7 digits to 8, but in my particular case it's sufficient:     &lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;eval myInt=if(len(myInt)==7,"0".myInt,myInt)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Anyone have any suggestions?   I see that eval does not have anything obvious here like a &lt;CODE&gt;zeropad&lt;/CODE&gt; function.  I wonder if it would be a good thing to add, or if there's some other way of doing this more cleanly,  or in such a way that it can add the correct number of zeros and not just one, with eval or with another command.    Any help appreciated. &lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2012 18:00:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37800#M8533</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2012-04-26T18:00:35Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37801#M8534</link>
      <description>&lt;P&gt;You can use rex to first pad the number with "enough" zeroes, then to trim it to the length you require.  I broke it into two parts since rex's sed mode doesn't seem to like concatenated commands; I don't know whether you consider this cleaner, but it does allow for variable-length numbers.  Let me know if this is useful.&lt;/P&gt;

&lt;P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;BR /&gt;
... | rex mode=sed field=myInt "s/(\d+)/00000000\1/" | rex mode=sed field=myInt "s/0*([0-9]{8})/\1/" &lt;BR /&gt;
&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2012 19:14:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37801#M8534</guid>
      <dc:creator>cphair</dc:creator>
      <dc:date>2012-04-26T19:14:55Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37802#M8535</link>
      <description>&lt;P&gt;Awesome.  No, I like that better than mine.  I'll leave the question open for another couple days in case someone has a third way.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2012 22:17:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37802#M8535</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2012-04-26T22:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37803#M8536</link>
      <description>&lt;P&gt;How about&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;* | eval myInt="00000000000".tostring(myInt) | eval length=len(tostring(myInt)) | eval trimLength=tonumber(length-3) | eval myInt=tonumber(substr(myInt,trimLength))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;There may be some small errors in there as I haven't had the time to test that. In essence, it prepends a bunch of zeros, finds the difference between the length of the string with all the zeros and the total length you want (I chose 3 in the example), and then uses substr() to return only that many digits from the right end of the string.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Apr 2012 23:01:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37803#M8536</guid>
      <dc:creator>rtadams89</dc:creator>
      <dc:date>2012-04-26T23:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37804#M8537</link>
      <description>&lt;P&gt;Not bad.  #1) - the 'tonumber' on the end ends up removing all the hard-earned zeros.   #2), in my case my values are in hex so I can't use any of the int() stuff. &lt;/P&gt;

&lt;P&gt;However you're close.   This is similar and works very well. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval myVal="1A09" | eval initialLength=len(tostring(myVal)) | eval myVal="0000000000".tostring(myVal) | eval myVal=substr(myVal,initialLength,10)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 26 Apr 2012 23:13:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37804#M8537</guid>
      <dc:creator>sideview</dc:creator>
      <dc:date>2012-04-26T23:13:35Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37805#M8538</link>
      <description>&lt;P&gt;How about this - keeps it simple. Use &lt;CODE&gt;eval&lt;/CODE&gt; to tack on as many zeroes as you'll ever need, then &lt;CODE&gt;rex&lt;/CODE&gt; to get the last x amount of digits.&lt;/P&gt;

&lt;P&gt;Regular&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval myint="000000".myint | rex field=myint "(?&amp;lt;myint&amp;gt;\d{6})$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hex&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval myhex="00000000".myhex | rex field=myhex "(?&amp;lt;myhex&amp;gt;[0-9A-Fa-f]{8})$"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Oct 2013 08:52:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37805#M8538</guid>
      <dc:creator>Jason</dc:creator>
      <dc:date>2013-10-07T08:52:12Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37806#M8539</link>
      <description>&lt;P&gt;I think the "most correct" option (which does not exist btw) would be a printf-like printed-format function - something like...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval newfield=printf("%04.4f %-30s",fieldone, fieldtwo)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I filed an ER asking for this earlier this year - case # 113961.  Feel free to tack on a "me too" ER case.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Oct 2013 17:17:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37806#M8539</guid>
      <dc:creator>dwaddle</dc:creator>
      <dc:date>2013-10-07T17:17:51Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37807#M8540</link>
      <description>&lt;P&gt;Yes, that looks closest to a "zerofill" function, or "format" function or something. I don't think I filed an ER.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2013 04:46:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37807#M8540</guid>
      <dc:creator>Jason</dc:creator>
      <dc:date>2013-10-08T04:46:50Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37808#M8541</link>
      <description>&lt;P&gt;Assuming I know the numbers are always positive and below a certain length, I use a SUBSTR function.  For example, to pad out to a 4-digit value&lt;BR /&gt;
   eval formattedNumber=substr("0000".rawNumber,-4)&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2015 15:16:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37808#M8541</guid>
      <dc:creator>jhodgepason</dc:creator>
      <dc:date>2015-12-17T15:16:03Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37809#M8542</link>
      <description>&lt;P&gt;I downvoted this post because it is not a solution, its a would be nice if this works&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 01:43:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37809#M8542</guid>
      <dc:creator>AdamParuk</dc:creator>
      <dc:date>2017-02-08T01:43:07Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37810#M8543</link>
      <description>&lt;P&gt;We don't usually downvote for items like this, saving downvoting for things that are plainly &lt;EM&gt;wrong&lt;/EM&gt; or might break people's systems. This forum is intended as a place to share and communicate various solutions, and occasionally 'Yes, I filed an enhancement request for this, please join it' is sometimes completely valid.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Feb 2017 14:42:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37810#M8543</guid>
      <dc:creator>Richfez</dc:creator>
      <dc:date>2017-02-08T14:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37811#M8544</link>
      <description>&lt;P&gt;This worked perfect for me!&lt;/P&gt;</description>
      <pubDate>Fri, 07 Apr 2017 14:54:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37811#M8544</guid>
      <dc:creator>nickbloglovin</dc:creator>
      <dc:date>2017-04-07T14:54:43Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37812#M8545</link>
      <description>&lt;P&gt;Good news: this eval function was added in Splunk 6.6 (released today)&lt;/P&gt;</description>
      <pubDate>Tue, 02 May 2017 15:42:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37812#M8545</guid>
      <dc:creator>mitch_1</dc:creator>
      <dc:date>2017-05-02T15:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37813#M8546</link>
      <description>&lt;P&gt;Thanks Mitch and all the other Splunkers.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jun 2019 13:37:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37813#M8546</guid>
      <dc:creator>enno</dc:creator>
      <dc:date>2019-06-27T13:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: is there a cleaner way to zeropad numeric values, ie add leading zeros?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37814#M8547</link>
      <description>&lt;P&gt;Using &lt;CODE&gt;printf&lt;/CODE&gt; conversion function this is possible using something like &lt;CODE&gt;printf("%04d",fieldName)&lt;/CODE&gt;. Refer to documentation: &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/ConversionFunctions#Flag_characters"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/ConversionFunctions#Flag_characters&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Following is a run anywhere example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults
| fields - _time
| eval data=1
| eval padded_data= printf("%04d",data)
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Jun 2019 13:50:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/is-there-a-cleaner-way-to-zeropad-numeric-values-ie-add-leading/m-p/37814#M8547</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2019-06-27T13:50:45Z</dc:date>
    </item>
  </channel>
</rss>

