How to generate random string from specific pattern?

New to Debian (Or Linux in general)? Ask your questions here!

How to generate random string from specific pattern?

Postby hack3rcon » 2019-07-27 07:50

Hello.
How can I generate random string from a specific pattern? For example, My pattern is "Hey, Hello World!" and I want generate it randomly. Something like:
Code: Select all
Hello World! Hey, Hello, Hey World!


Thanks.
hack3rcon
 
Posts: 307
Joined: 2015-02-16 09:54

Re: How to generate random string from specific pattern?

Postby Bloom » 2019-07-27 08:10

Not exactly random, but perhaps this helps?
Code: Select all
echo "Hey, Hello World!" | tr ' ' '\n' | shuf | tr '\n' ' '
User avatar
Bloom
 
Posts: 194
Joined: 2017-11-11 12:23

Re: How to generate random string from specific pattern?

Postby hack3rcon » 2019-07-27 09:13

Bloom wrote:Not exactly random, but perhaps this helps?
Code: Select all
echo "Hey, Hello World!" | tr ' ' '\n' | shuf | tr '\n' ' '

Thanks.
How can I generate it to a certain number?
hack3rcon
 
Posts: 307
Joined: 2015-02-16 09:54

Re: How to generate random string from specific pattern?

Postby Bloom » 2019-07-27 10:12

This will shuffle the words in the sentence you specify, so the total length will always be the same.
The following code will give you actual random alphanumeric characters en here you can specify the length:
Code: Select all
strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 8 | tr -d '\n'; echo;

This will yield a 8 character response. If you want a different number, just replace the 8 with the number wanted.
User avatar
Bloom
 
Posts: 194
Joined: 2017-11-11 12:23


Return to Beginners Questions

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable