I am having trouble getting my sed syntax down to add a varying number of leading zeros to a numeric organizational scheme. The strings I am operating on appear like,Some Text Here

leveraging the sed syntax

sed -r ":r;s/\b[0-9]{1,$((1))}\b/0&/g;tr"

I am able to elicit the response,Some Text Here

However, What I am looking for is something to zero-fill up to 2 digits in fields 2 and 3 and 3 digits in field 4 so that all items are of a standard length at [0-9].[0-9]{2}.[0-9]{2}.[0-9]{3},Some Text Here

For the life of me I cannot figure even how to modify the boundary to include the parameters necessary to snap to only numerals following a period. I think it has something to do with the use of the \b which I understand matches zero characters at a word boundary, but I do not understand why my attempts to add a period to the match fail as follows:

sed -r ":r;s/\.\b[0-9]{1,$((1))}\b/0&/g;tr"
sed -r ":r;s/\b\.[0-9]{1,$((1))}\b/0&/g;tr"
Both cause the statement to hang

sed -r ":r;s/\b[0-9]\.{1,$((1))}\b/0&/g;tr"
sed -r ":r;s/\b[0-9]{1,$((1))}\.\b/0&/g;tr"
sed -r ":r;s/\b[0-9]{1,$((1))}\b\./0&/g;tr"
cause the statement to output:,Some Text Here

Additionally, I expect that I will have additional problems if the statement contains text like:,Some Number 1 Here

It is a foregone conclusion that I need to really learn sed and all of its complexities. I am working on that, but expect that this particular statement will continue to cause me trouble for a while. Any help would be greatly appreciated.

EDIT: I've figured out a way... This statement seems to do what I am looking for, but there has got to be a more elegant way to do this.

sed -r ':r;s/\b[0-9]{1,1}\.\b/0&/;tr;:i;s/\b[0-9]{1,2},\b/0&/;ti;s/.//'

Also, syntactically this will cause problems if a similar number format appears in the text... similar to:,Some Text Referring to Document XXX Heading 1.2.3

In which case it will result in:,Some Text Referring to Document XXX Heading 01.02.03

Solved Thank you all for your help here. I initially solved the problem with the answer I accepted below. I've sense moved the solution into Python as a part of a larger solution leveraging the sort below:

def getPaddedKey(line):
    keyparts = line[0].split(".")
    keyparts = map(lambda x: x.rjust(5, '0'), keyparts)
    return '.'.join(keyparts)

s=sorted(reader, key=getPaddedKey)
  • This seems to do what I am looking for: sed -r ':r;s/\b[0-9]{1,1}\.\b/0&/;tr;:i;s/\b[0-9]{1,2},\b/0&/;ti;s/.//' However, I'd love to know if there is a more elegant approach.
    – daijizai
    Jul 18, 2017 at 18:39
  • 1
    Strangely, reversing the string, applying trailing zeros, and then reversing the result may achieve your aim more easily. Jul 18, 2017 at 18:51
  • 2
    Using printf (or a printf call within Awk) may be more straightforward.
    – Wildcard
    Jul 18, 2017 at 21:51
  • 1
    this is definitely something that will be easier to implement, read, understand, and modify in the future in a language like awk or perl (or anything else that has printf and easy field-splitting).
    – cas
    Jul 19, 2017 at 0:15
  • 1
    @Wildcard - point well taken. Can you point me to something about debugging sed? Usually, I resort to prolonged staring punctuated by expletives. ;) Barring that, I sometimes break a sed statement into smaller pieces and try to get each one to work before combining them again. I recently read a great tutorial github.com/learnbyexample/Command-line-text-processing/blob/… and I was sure some of the examples were wrong until I applied prolonged staring.
    – Joe
    Jul 22, 2017 at 4:19

5 Answers 5


bash can handle this. It'll be a lot slower than perl though:

echo ",Some Text Here" | 
while IFS=., read -r a b c d text; do
    printf "%d.%02d.%02d.%03d,%s\n" "$a" "$b" "$c" "$d" "$text"
done,Some Text Here

You haven't specifically asked for a perl solution but here's one anyway. Personally I think this is a little easier to read, especially when broken into several lines.

First here is the one-liner:

    echo ',Some Text Here'
    echo ',Some Text Here'
    echo ',Some Number 1 Here'
    echo ',Some Text Referring to Document XXX Heading 1.2.3'
    echo ',Some \n \s \text'
) |
perl -ne '($ip, $text) = split(/,/, $_, 2); $ip = sprintf("%1d.%02d.%03d.%03d", split(/\./, $ip)); print "$ip,$text"'

Its results:,Some Text Here,Some Text Here,Some Number 1 Here,Some Text Referring to Document XXX Heading 1.2.3,Some \n \s \text

And here is the perl script broken out and commented (the -n flag puts an implicit while read; do ... done loop around the code):

($ip, $text) = split(/,/, $_, 2);                # Split line into two parts by comma
@octets = split(/\./, $ip)                       # Split IP address into octets by dots
$ip = sprintf("%1d.%02d.%03d.%03d", @octets);    # Apply the formatting
print "$ip,$text"                                # Output the two parts
  • Ironically, I was just about to give up in sed and move to awk when you posted this. It seems to fit the bill. I'll check it and get back.
    – daijizai
    Jul 18, 2017 at 18:48
  • @daijizai awk would work too - same principle using printf Jul 18, 2017 at 18:49
  • The only thing this fails at I couldn't have anticipated, but is significant. It seems to strip backslash from the text portion.
    – daijizai
    Jul 18, 2017 at 19:50
  • @daijizai not here it doesn't. How are you feeding it text with a backslash? I've added a backslashed example for you Jul 18, 2017 at 20:03
  • In my use with my internal dataset there are rows with the text column containing strings like SOME\Text\Might\Be\Here\4Realz. When this dataset was passed to the perl statement it resulted in a response like SOMETextMightBeHere4Realz
    – daijizai
    Jul 20, 2017 at 4:32

Usage: leading_zero.sh input.txt


sed -r '
' "$1"


  1. First subtitution add certain amount of zeros to each number. 1 zero to 2 and 3 numbers, 2 zero to 4 number. Doesn't matter, how much digits already there are.
  2. Second substution removes all extra zeros, leaving only needed amount of numbers. 2 and 3 numbers should be contain only 2 digits. Leaves them and removes rests. Fourth number should be contain only 3 digits. Leaves them and removes rests.

input.txt,Some Text Here,Some Text Here,Some Text Referring to Document XXX Heading 1.2.3,Some Text Here,Some Text Referring to Document XXX Heading 1.2.3,Some Text Here

output.txt,Some Text Here,Some Text Here,Some Text Referring to Document XXX Heading 1.2.3,Some Text Here,Some Text Referring to Document XXX Heading 1.2.3,Some Text Here
  • While in the end I just ended up scripting this in Python for expediency, this is the best answer to my question as written given that the perl previously submitted removed backslashes (at least) from the output. This 1. is a sed solution, and 2. produces the proper output without molestation of the text. Marking as answer. Thanks! :-)
    – daijizai
    Jul 18, 2017 at 21:52
  • @daijizai as I have already demonstrated, the perl version does not remove backslashes. Jul 19, 2017 at 22:24

Here's one possible approach:
sed -E 's/([0-9]*\.)/0\1/g;s/.//;s/([0-9]*,)/00\1/'


echo ",Some Text Here" | sed -E 's/([0-9]*\.)/0\1/g;s/.//;s/([0-9]*,)/00\1/'
1.011.0111.001111,Some Text Here

Also work with this string:

echo ",Some Number 1 Here" | sed -E 's/([0-9]\.)/0\1/g;s/.//;s/([0-9],)/00\1/',Some Number 1 Here

...and this string:

echo "1.2.2101.7191,Some Text Here" | sed -E 's/([0-9]*\.)/0\1/g;s/.//;s/([0-9]*,)/00\1/'
1.02.02101.007191,Some Text Here
  • Unfortunately this breaks down as the numerals climb. For instance:,Some Text Here Became:,Some Text Here
    – daijizai
    Jul 18, 2017 at 17:29
  • @daijizai Please see my edit. Would this meet the requirement? Jul 18, 2017 at 17:38
  • Unfortunately not, but I think that might be my fault. The zero-fill needs of be up two two digits on field 2 and 3 and 3 digits on field 4. Essentially [0-9].[0-9]{2}.[0-9]{2}.[0-9]{3},Some Text Here
    – daijizai
    Jul 18, 2017 at 18:11
perl -pe '/^\d/g && s/\G(?:(\.\K\d+(?=\.))|\.\K\d+(?=,))/sprintf "%0".($1?2:3)."d",$&/ge'


The method used is here is to look at the neighborhoods of the numerics and take action based on that. So, the 2nd and 3rd numbers see a dot on both sides whilst the 4th numeric sees dot on it's left and a comma to it's right.

The $1 is set when the regex takes the path of 2nd or 3rd nums and accordingly the precision padding is 2. OTOH, for the 4th num, the padding is 3.

% cat file.txt,Some Text Here,Some Text Here,Some Number 1 Here,Some Text Referring to Document XXX Heading,Some \n \s \text

Results:,Some Text Here,Some Text Here,Some Number 1 Here,Some Text Referring to Document XXX Heading,Some \n \s \text

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .