Refine your search:

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.

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:

eval myInt=if(len(myInt)==7,"0".myInt,myInt)

Anyone have any suggestions? I see that eval does not have anything obvious here like a zeropad 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.

asked 26 Apr '12, 11:00

sideview's gravatar image

sideview ♦
25.6k4543
accept rate: 46%

edited 26 Apr '12, 11:01


2 Answers:

How about * | eval myInt="00000000000".tostring(myInt) | eval length=len(tostring(myInt)) | eval trimLength=tonumber(length-3) | eval myInt=tonumber(substr(myInt,trimLength))

There may be some small errors in there as I havn't had tiem to test that. In essence, it prepends a bucn hof zeros, finds the differenc between the lenght of the string with all the zeros and the total lenth 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.

link

answered 26 Apr '12, 16:01

rtadams89's gravatar image

rtadams89
44925
accept rate: 34%

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.

However you're close. This is similar and works very well. | eval myVal="1A09" | eval initialLength=len(tostring(myVal)) | eval myVal="0000000000".tostring(myVal) | eval myVal=substr(myVal,initialLength,10)

(26 Apr '12, 16:13) sideview ♦

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.


... | rex mode=sed field=myInt "s/(\d+)/00000000\1/" | rex mode=sed field=myInt "s/0*([0-9]{8})/\1/" 
link

answered 26 Apr '12, 12:14

cphair's gravatar image

cphair
98326
accept rate: 51%

1

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.

(26 Apr '12, 15:17) sideview ♦
Post your answer
toggle preview

Follow this question

Log In to enable email subscriptions

RSS:

Answers

Answers + Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×396
×240

Asked: 26 Apr '12, 11:00

Seen: 759 times

Last updated: 26 Apr '12, 16:13

Copyright © 2005-2012 Splunk Inc. All rights reserved.