Need help with Bash

LynxGeekNYC

New Member
Joined
Jun 6, 2020
Messages
2
Reaction score
0
Credits
20
Hello,

Got a Bash Scripting issue. It has to be done with bash.

I've been trying everything from SED to other tricks... Can't seem to get it right.
I need to be able to replace a whole line with a random number and prefix in a text file. Example:

ABCD123456

The letters are the preset prefix and the numbers are generated randomly but the first number can't be smaller than 3.

Anyone know how this can be done in bash?

Appreciate anyone help! Thanks!
 
Last edited:


Homework? Assignment? Exam Question?

What have YOU done so far?
 
I tried SED and AWK different variations. Not HW. It's for my firm and we need to create random generated Case file numbers.
 
Let us see what you have done with SED and AWK. Please show us your attempts.
 
re: random number generation:
If it’s going to be a 6 digit random number and the first number must be 3 or greater, you can generate the first digit of the random number from /dev/random (or /dev/urandom) using od like this:
Bash:
digit1=$(($(od -vAn -N4 -tu4 < /dev/random)%7+3))

Using Modulo 7 with a random number from /dev/random will generate a digit that is between 0 and 6. If we add 3 to that, that will guarantee that the first digit is between 3 and 9 (inclusive).

Then you could use od again to generate the other 5 digits randomly like this:
Bash:
randomDigits=$(($(od -vAn -N4 -tu4 < /dev/random)))
digits=${randomDigits:1:5}
The above will generate a number that is longer than 5 digits, so we just use the first 5 digits of the generated number.

Then your replacement string becomes:
Bash:
newString="$prefix$digit1$digits"
 

Members online


Top