3digit block separated by a dot and a hyphen


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 3digit block separated by a dot and a hyphen
# 1  
Old 03-19-2016
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

Code:
echo $RANDOM$RANDOM$RANDOM

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

Code:
sed i\.

makes a dot in front of the output. Before that I tried something like
Code:
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)
Code:
array=(This is a text)
               echo ${array [0]:2:2}

ANY HINTS ? Thanks in advance!

Last edited by 1in10; 03-19-2016 at 07:39 PM.. Reason: solved
# 2  
Old 03-19-2016
What's wrong with
Code:
echo $RANDOM.$RANDOM.$RANDOM
31533.27416.10085

?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-19-2016
Would that work?
Code:
printf "%.3s.%.3s.%.3s-%.2s\n" $RANDOM, $RANDOM, $RANDOM, $RANDOM
141.470.200-20

This User Gave Thanks to Aia For This Post:
# 4  
Old 03-19-2016
Quote:
Originally Posted by Aia
Would that work?
Code:
printf "%.3s.%.3s.%.3s-%.2s\n" $RANDOM, $RANDOM, $RANDOM, $RANDOM
141.470.200-20

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:
Code:
127.258.34,-2,

But, that problem can easily be fixed using the same logic with something like:
Code:
printf '%03d.%03d.%03d-%02d\n' $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))

which luckily produced the output:
Code:
065.154.834-83

the first time I tried it (note the leading zero).
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 03-19-2016
Hi.

If a conveniently-specified range is useful:
Code:
$ printf "%.3s.%.3s.%.3s-%.2s\n" $(shuf -i 1-255 -n 3) $(shuf -i 0-59 -n 1)

Resulting in, for example:
Code:
54.152.170-4

On a system like:
Code:
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
This User Gave Thanks to drl For This Post:
# 6  
Old 03-22-2016
the final result that works on linux is, at least for me, the following one:
Code:
printf "%03d.%03d.%03d.-%02d\n" $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))

thanks to all, I appreciate it!!!
# 7  
Old 03-22-2016
Quote:
Originally Posted by 1in10
the final result that works on linux is, at least for me, the following one:
Code:
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:
Code:
printf "%03d.%03d.%03d-%02d\n" $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))

instead of:
Code:
printf "%03d.%03d.%03d.-%02d\n" $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%1000)) $((RANDOM%100))
----------------------^

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Deleting directory with leading hyphen in name

I asked this question last month in Stack Exchange (linux - delete directory with leading hyphen - Server Fault) and none of the answers supplied worked. I have somehow created a directory with a leading hyphen and cannot get rid of it. # ls -li | grep p 2621441 drwxr-xr-x. 2 root root... (4 Replies)
Discussion started by: edstevens
4 Replies

2. Shell Programming and Scripting

Insert a hyphen between two delimiters using sed

Hey guys, I have a file that is delimited by | and I am trying to write a sed command to convert this: abc|def||ghi|jkl||||mnop into this: abc|def|-|ghi|jkl|-|-|-|mnop The output I am getting out of: sed -e "s/+//g" /tmp/opt.del > /tmp/opt2.del is like: ... (9 Replies)
Discussion started by: prohank
9 Replies

3. Shell Programming and Scripting

sed - replacing a substring containing a hyphen

I'm attempting to replace a substring that contains a hyphen and not having much success, can anyone point out where i'm going wrong or suggest an alternative. # echo /var/lib/libvirt/images/vm888b-clone.qcow | sed -e 's|vm888-clone|qaz|g' /var/lib/libvirt/images/vm888b-clone.qcow (1 Reply)
Discussion started by: squrcles
1 Replies

4. Shell Programming and Scripting

Combining multiple block of lines in one comma separated line

Hi Everyone, On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next... (7 Replies)
Discussion started by: gr8_usk
7 Replies

5. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

6. Shell Programming and Scripting

awk - replace first hyphen

How do I use awk to replace the first hyphen of a specific record? (1 Reply)
Discussion started by: locoroco
1 Replies

7. Shell Programming and Scripting

delete the last hyphen

I want to check for more than one hyphen and then hold the first one and delete the rest of the hyphen. I try something like this sed 's/\(*\)\1/\1/' but this doesn't work. I try something like this sed 's/\(*\)-\1/ \1/g' but here the script delete all the hyphen. I want to go from this : I... (2 Replies)
Discussion started by: thailand
2 Replies

8. Shell Programming and Scripting

Hyphen char after shebang notation

Hi, I have a trivial question to ask, I am seeing in some shell scripts the '-' (hyphen) character following the first line of shell script (i.e) the shebang notation as follows: #!/bin/sh - #! /bin/bash - what does the hyphen signify? What will happen if it is not given explicitly? (2 Replies)
Discussion started by: royalibrahim
2 Replies

9. Shell Programming and Scripting

add a hyphen every 2 characters of every line

I have a text file like this with hundreds of lines: >cat file1.txt 1027123000 1027124000 1127125000 1128140000 1228143000 > all lines are very similar and have exactly 10 digits. I want to separate the digits by twodigit and hyphens....like so, > 10-27-12-30-00 10-27-12-40-00... (7 Replies)
Discussion started by: ajp7701
7 Replies

10. Shell Programming and Scripting

use of hyphen in #! line

In one script i have seen - in #! line can somebody explain the meaning of -(hyphen) here #! /bin/sh - (7 Replies)
Discussion started by: Dhruva
7 Replies
Login or Register to Ask a Question