Results 1 to 10 of 10

Thread: Bash script question (II)

  1. #1
    Join Date
    Nov 2007
    Beans
    69

    Bash script question (II)

    How can I do to set a determined number of leading zeros to rise a defined number of characters. Let me explain myself:

    I have numbers from 1 to 1000, but I need them to count 9 characters, so it would be like:

    000000001
    ...
    000000013
    ...
    000000104

    (the number of leading zeros change).

    And another question is: Is it possible to change the order of the numbers to an the order I want them to be? How?

    i.e. 12345 to 53241
    62130 to 01236

    Thank you!

  2. #2
    Join Date
    Oct 2009
    Location
    here and there...
    Beans
    67
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash script question (II)

    i'm sure there is a much better method but here's my basic 'dumb' method:
    Code:
    fc () { for (( c=${#1}; c<9; c++ )); do return="${return}0"; done; echo ${return}$1; }
    You just call the function like this: fc 2351 and the output will be 000002351
    if you want another number of leading zeros just change the c<9 condition to another number

    as for the second question: wikipedia
    Last edited by blchinezu; June 8th, 2010 at 09:24 PM.

  3. #3
    Join Date
    Nov 2007
    Beans
    69

    Re: Bash script question (II)

    Going to try. Thank you!

  4. #4
    Join Date
    Apr 2006
    Location
    Atlanta, USA
    Beans
    427

    Re: Bash script question (II)

    Code:
    printf "%09d\n" 142
    Here we are, trapped in the amber of the moment. There is no why.

  5. #5
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash script question (II)

    Code:
    printf "%09d\n" 104
    000000104
    help printf
    For the second question, I don't see any pattern to the ordering you want, though you can use substring expansion to separate each character of the string.
    Code:
    string=62130
    echo "${string:0:1}" # first char
    echo "${string:2:1}" # third char
    http://mywiki.wooledge.org/BashFAQ/073

  6. #6
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash script question (II)

    Hi,
    for the first requirement, a variation on the printf solutions, which generates the list in one go:
    Code:
    seq -f "%09g" 1 1000
    for the second requirement, I think this does what you want:
    Code:
    $ for number in 12345 62130 
    > do
    > echo $number | sed -r 's/(.)(.)(.)(.)(.)/\5\3\2\4\1/'
    > done
    53241
    01236

  7. #7
    Join Date
    Oct 2009
    Location
    here and there...
    Beans
    67
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash script question (II)

    as i said.. there has to be an easier way to do it... there always is

  8. #8
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash script question (II)

    Quote Originally Posted by DaithiF View Post
    Hi,
    for the first requirement, a variation on the printf solutions, which generates the list in one go:
    Code:
    seq -f "%09g" 1 1000
    I'd still use printf; it's defined by POSIX and is a built-in in bash. seq is non-standard.

    Code:
    printf "%09d\n" {1..1000}
    In bash4 you can even generate that list with brace expansion
    Code:
    for i in {000000001..000001000}; do ...
    http://mywiki.wooledge.org/BashFAQ/018
    Last edited by geirha; June 8th, 2010 at 10:49 PM.

  9. #9
    Join Date
    Jun 2008
    Location
    UK
    Beans
    282
    Distro
    Xubuntu 16.04 Xenial Xerus

    Re: Bash script question (II)

    If you install ksh93 and check the man page it is possible to use
    Code:
    set -i var
    with an option to make var an integer with a set number of leading zeroes. Zsh may also have this functionality.

    As for the reversing of digits, the sed option looks good to me, but for an arbitrary number of characters or digits:
    PHP Code:
    a=12345
    b
    =
    c=
    while -
    "$a"
    do
       
    b="${a#.}" # first char of $a
       
    c="$c$b"   # append to $c 
       
    a="${a/$b/} # remove $b from $a
    done 
    Untested.

    Andrew

  10. #10
    Join Date
    Jun 2008
    Location
    California
    Beans
    2,271
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Bash script question (II)

    Here's another approach that will do what you want. The number and number order are input as command-line parameters.

    Code:
    #!/bin/bash
    
    printf $2 | while read -n 1 char ; do
      printf ${1:((--char)):1}
    done
    
    echo
    For example,

    $ testscript 62130 53241
    01236
    Last edited by kaibob; June 10th, 2010 at 05:11 AM.

Bookmarks

Posting Permissions

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