Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 17
Hi, I was wondering if there was a command to zero pad a number on the left side? Thanks...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    5

    How to zero pad a number


    Hi,

    I was wondering if there was a command to zero pad a number on the left side?

    Thanks

  2. #2
    Linux User
    Join Date
    Aug 2005
    Posts
    408
    I'm not sure I fully understand your question. Did you want to do this within a text file? If you do, you could probably use sed to do something like that.

    Depending on the constraints involved, something like this:
    Code:
    sed -e 's/^\([0-9]\)/0\1/g' samplefile
    Will turn this:
    Code:
    893756982736
    8972348596298762
    09689273456982756
    817984718942
    1872412587165
    n 32956928356
      9u  98u6
    u8459218204
    009325661361
    .103659161
    Into this:
    Code:
    0893756982736
    08972348596298762
    009689273456982756
    0817984718942
    01872412587165
    n 32956928356
      9u  98u6
    u8459218204
    0009325661361
    .103659161
    This may not be exactly what you want, however.

  3. #3
    Just Joined!
    Join Date
    Jun 2006
    Posts
    5
    I mean like

    345
    44
    5435345
    434

    into

    0000345
    0000044
    5435345
    0000434

    Is there some library that I could use for reference?

  4. #4
    Linux User tech_man's Avatar
    Join Date
    Jan 2007
    Location
    USA
    Posts
    385
    ^^^ Those zeros should be ignored shouldn't they?
    'Tis better to be silent and be thought a fool, than to speak and remove all doubt.'
    --Abraham Lincoln

  5. #5
    Just Joined!
    Join Date
    Jun 2006
    Posts
    5
    I don't want them to be ignored. That's the purpose of zero padding.

    Does anyone have any suggestions?

    Thanks

  6. #6
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Since I use Perl for just about everything, here's my solution in Perl:

    Code:
    my $num_cols; # Is this fixed, or does it depend on the longest number?
    
    while(<>) {
        chomp;
        print '0' x ($num_cols - length);
        print;
        print "\n";
    }
    Note that this code reads from standard input/files provided as arguments and spits out the result to standard output.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

  7. #7
    Linux User
    Join Date
    Oct 2004
    Location
    /dev/random
    Posts
    404
    You could simply use printf() (in both C and perl) for printing the output with leading zeroes.
    For example,
    Code:
    printf("%06d", num)
    will print the number with leading zeroes and field width of 6 (min - if the no. has less digits than this width, it will be zero prepended).

    HTH
    The Unforgiven
    Registered Linux User #358564

  8. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Gah! I thought there was a way to use printf, and there we go!

    So the easiest way I can think of to do this would be:
    Code:
    #!/usr/bin/perl
    
    use strict;
    
    chomp(my @lines = <>);
    
    my $longest_length;
    
    for $line (@lines)
    {
        $longest_length = length($line) if length($line) > $longest_length;
    }
    
    for $line (@lines)
    {
        printf('%0' . $longest_length - length($line) . "d\n", $line);
    }
    This also reads from stdin (unless you provide files on the command line) and prints to stdout.

  9. #9
    Just Joined!
    Join Date
    Jun 2006
    Posts
    5
    woops... I forgot to mention that I am doing this is shell script

    It's supposed to be based on the length of the largest one (not fixed), but I can easily determine that already.

  10. #10
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Slackware, {Free, Open, Net}BSD, Solaris
    Posts
    1,288
    Hi.

    Almost the same ... cheers, drl
    SYNOPSIS
    printf FORMAT [ARGUMENT]...
    printf OPTION

    DESCRIPTION
    NOTE: your shell may have its own version of printf which will
    supercede the version described here. Please refer to your shell's doc-
    umentation for details about the options it supports.

    Print ARGUMENT(s) according to FORMAT.
    -- excerpt from man -S1 printf
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •