Flag/command to shuffle the letters of a word

I need to create a script, that will read a word as an input and provide the output as shuffled letters of the word.

Eg: Input word as "Azharudeen" and output should be as "zAhauredne"

2 Answers

You could do this with a short Python script specified on the command line:

$ python -c "import random,sys; chars=list(sys.argv[1]); random.shuffle(chars); print(''.join(chars))" Azharudeen
neuAazrhed
2

Not the most elegant, but this works:

$ echo `echo "Azharudee" | sed -e 's/./&\n/g' | shuf` | sed -e 's/ //g'
Auerdzhae

Edit (for explanation):

echo "Azharudee"

puts that word into standard output, which is piped into

sed -e 's/./&\n/g'

which places each character onto its own line, by replacing each character by itself followed by a newline character, and pipes it into

shuf

which randomly shuffles the lines it is given and puts them on standard output.

Meanwhile this outer layer wraps all the above (as "...") in

echo `...` | sed -e 's/ //g'

The "echo" causes the multiple lines produced by the first set of commands to be combined into a single line, and the "sed" replaces all the spaces with nothing.

Note that it might have been visually better had I used $(echo ... shuf) rather than enclosing it in grave accents, which have the same effect but are harder to see.

2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like