3digit block separated by a dot and a hyphen

once again I need a hint how to operate a dot and a hyphen.
my aim is to use

echo $RANDOM$RANDOM$RANDOM

giving me a bunge of numbers. I need to seperate them into yyy.xxx.zzz-tt

sed i\.

makes a dot in front of the output. Before that I tried something like

jot -r -n 8 0 9 | rs -q 0

or even more daring the apg-command
and last but not least working out an array e.g.(this is just an example how it could be done)

array=(This is a text)
               echo ${array [0]:2:2}

ANY HINTS ? Thanks in advance!

What's wrong with

echo $RANDOM.$RANDOM.$RANDOM
31533.27416.10085

?

1 Like

Would that work?

printf "%.3s.%.3s.%.3s-%.2s\n" $RANDOM, $RANDOM, $RANDOM, $RANDOM
141.470.200-20
1 Like

This runs a risk of having some one and two digit numbers followed by a comma in cases where $RANDOM expands to a value less than 100. For instance, sticking your command in a while loop for a couple of seconds and grepping the output for commas produce a couple of dozen lines of output including:

127.258.34,-2,

But, that problem can easily be fixed using the same logic with something like:

printf '%03d.%03d.%03d-%02d\n' $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))

which luckily produced the output:

065.154.834-83

the first time I tried it (note the leading zero).

1 Like

Hi.

If a conveniently-specified range is useful:

$ printf "%.3s.%.3s.%.3s-%.2s\n" $(shuf -i 1-255 -n 3) $(shuf -i 0-59 -n 1)

Resulting in, for example:

54.152.170-4

On a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.3 (jessie) 
shuf (GNU coreutils) 8.23

Best wishes ... cheers, drl

1 Like

the final result that works on linux is, at least for me, the following one:

printf "%03d.%03d.%03d.-%02d\n" $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))

thanks to all, I appreciate it!!!

That won't get the output format you said you wanted. You have an extra period in your format string. I think you want:

printf "%03d.%03d.%03d-%02d\n" $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))

instead of:

printf "%03d.%03d.%03d.-%02d\n" $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))
----------------------^
1 Like