PDA

View Full Version : Bash script question (II)



j_arquimbau
June 8th, 2010, 09:02 PM
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!

blchinezu
June 8th, 2010, 09:08 PM
i'm sure there is a much better method but here's my basic 'dumb' method:


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 (http://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations)

j_arquimbau
June 8th, 2010, 09:48 PM
Going to try. Thank you!

johnl
June 8th, 2010, 10:03 PM
printf "%09d\n" 142

geirha
June 8th, 2010, 10:08 PM
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.

string=62130
echo "${string:0:1}" # first char
echo "${string:2:1}" # third char

http://mywiki.wooledge.org/BashFAQ/073

DaithiF
June 8th, 2010, 10:24 PM
Hi,
for the first requirement, a variation on the printf solutions, which generates the list in one go:

seq -f "%09g" 1 1000

for the second requirement, I think this does what you want:

$ for number in 12345 62130
> do
> echo $number | sed -r 's/(.)(.)(.)(.)(.)/\5\3\2\4\1/'
> done
53241
01236

blchinezu
June 8th, 2010, 10:43 PM
as i said.. there has to be an easier way to do it... there always is :)

geirha
June 8th, 2010, 10:47 PM
Hi,
for the first requirement, a variation on the printf solutions, which generates the list in one go:

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.


printf "%09d\n" {1..1000}
In bash4 you can even generate that list with brace expansion

for i in {000000001..000001000}; do ...
http://mywiki.wooledge.org/BashFAQ/018

apmcd47
June 8th, 2010, 11:14 PM
If you install ksh93 and check the man page it is possible to use
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:

a=12345
b=
c=
while -n "$a"
do
b="${a#.}" # first char of $a
c="$c$b" # append to $c
a="${a/$b/} # remove $b from $a
done
Untested.

Andrew

kaibob
June 9th, 2010, 02:40 AM
Here's another approach that will do what you want. The number and number order are input as command-line parameters.


#!/bin/bash

printf $2 | while read -n 1 char ; do
printf ${1:((--char)):1}
done

echo

For example,


$ testscript 62130 53241
01236