Find the answer to your Linux question:
Page 2 of 2 FirstFirst 1 2
Results 11 to 17 of 17
Right: a shell script is pretty easy, I just didn't want to deal with arrays in Bash. It should be fairly analogous, just with Bash constructs over Perl ones....
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #11
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252

    Right: a shell script is pretty easy, I just didn't want to deal with arrays in Bash. It should be fairly analogous, just with Bash constructs over Perl ones.

  2. #12
    Linux User
    Join Date
    Aug 2005
    Posts
    408
    This is interesting. I might try to write a bash version just because I need the practice, and an answer isn't immediately leaping to mind. Serious question for the student (me): Does it have to be done with an array?

  3. #13
    Linux User tech_man's Avatar
    Join Date
    Jan 2007
    Location
    USA
    Posts
    385
    Does the zero padding just have to done at the output?
    'Tis better to be silent and be thought a fool, than to speak and remove all doubt.'
    --Abraham Lincoln

  4. $spacer_open
    $spacer_close
  5. #14
    Just Joined!
    Join Date
    Mar 2007
    Posts
    1
    The easiest way to zero-pad in a shell script is using awk:

    Code:
    echo "60" | awk '{printf "%06d\n", $1;}'
    
    000060
    Cheers,
    Mark.

  6. #15
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    So here's the way I would go about this:

    You store your input into an array. You iterate once over the array to find the longest number. You then iterate over it again, outputting each number with the appropriate padding.

    If you already know the longest number (say it's a constant or something), then you don't need to use an array, since it's just one iteration.

    I can't think of any way to find the largest length and pad the numbers without doing two iterations. Theoretically you don't need to use an array (you could write the input to a file or something, and just keep re-reading it), but an array is the simplest way, I think.

  7. #16
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    in awk
    Code:
    # awk 'BEGIN { printf("%06d\n",23) }'
    000023

  8. #17
    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,303
    Hi.

    Without an array (strictly speaking):
    Code:
    #!/bin/sh
    
    # @(#) s2       Demonstrate printf zero-fill on left without an array.
    
    # Interchange these two lines to turn debugging output
    # on and off.
    
    debug=":"
    debug="echo"
    
    LENGTH=0
    NUMBERS=""
    
    # Pass one, read, save longest length, add items to string.
    # Read from STDIN, so re-direct, like:
    #  ./s2 < datafile
    
    while read number
    do
            THIS=${#number}
            if [ $THIS -gt $LENGTH ]
            then
                    LENGTH=$THIS
            fi
            NUMBERS="$NUMBERS $number"
    done
    
    $debug
    $debug " Maximum length is $LENGTH"
    $debug " Accumulated sub-strings in master string NUMBERS:"
    $debug " $NUMBERS"
    $debug
    
    # Tell shell to parse string into positional parameters.
    
    set -- $NUMBERS
    
    # Dance through the string, printing with maximum length.
    
    for i
    do
            printf "%0${LENGTH}d\n" $i
    done
    
    exit 0
    Producing:
    Code:
    % ./s2 < data1
    
     Maximum length is 7
     Accumulated sub-strings in master string NUMBERS:
      345 44 5435345 434
    
    0000345
    0000044
    5435345
    0000434
    Best wishes ... cheers, drl
    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 2 of 2 FirstFirst 1 2

Posting Permissions

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