Scramble words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scramble words
# 1  
Old 10-01-2011
Scramble words

If I have a file like:

Code:
one two three four five six seven eight nine ten eleven twelve thirteen

How can I use bash to scramble the words randomly, so that the sequence becomes different?

I am talking about something like:
Code:
four eight eleven two one thirteen ...


Last edited by locoroco; 10-01-2011 at 08:46 PM..
# 2  
Old 10-01-2011
Maybe not the best solution, but for something in pure shell, and a few minutes effort, it'll at lest serve as an example:

Code:
#!/usr/bin/env ksh
# or for bash put next line at top
#!/usr/bin/env bash

while read line
do
    typeset -a words=($line)  # tokens from line into a work array
    have=${#words[@]}         # number of words this line
    used=0                    # number we've printed
    while (( $used < $have ))   # while we still have words to do
    do
        n=$(( $RANDOM % $have ))   # pick a word 
        while (( $n < $have )) && [[ ${words[$n]} == "" ]]  # if we've used it find next unused word
        do
            n=$(( $n + 1 ))
            if (( $n > $have - 1 ))  # wrap to beginning if needed
            then
                n=0
            fi
        done

        printf "%s " ${words[$n]}    # print the random choice 
        words[$n]=""                  # prevent word from being reused
        used=$(( $used + 1 ))
    done

    printf "\n"
done

This reads lines from standard input and 'scrambles' the words on the line.

Last edited by agama; 10-01-2011 at 10:35 PM.. Reason: typo
This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in any quote position and then change the words

hi, i need to replace all words in any quote position and then need to change the words inside the file thousand of raw. textfile data : "Ninguno","Confirma","JuicioABC" "JuicioCOMP","Recurso","JuicioABC" "JuicioDELL","Nulidad","Nosino" "Solidade","JuicioEUR","Segundo" need... (1 Reply)
Discussion started by: benjietambling
1 Replies

3. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

4. Shell Programming and Scripting

Gawk gensub, match capital words and lowercase words

Hi I have strings like these : Vengeance mitt Men Vengeance gloves Women Quatro Windstopper Etip gloves Quatro Windstopper Etip gloves Girls Thermobite hooded jacket Thermobite Triclimate snow jacket Boys Thermobite Triclimate snow jacket and I would like to get the lower case words at... (2 Replies)
Discussion started by: louisJ
2 Replies

5. Shell Programming and Scripting

How count the number of two words associated with the two words occurring in the file?

Hi , I need to count the number of errors associated with the two words occurring in the file. It's about counting the occurrences of the word "error" for where is the word "index.js". As such the command should look like. Please kindly help. I was trying: grep "error" log.txt | wc -l (1 Reply)
Discussion started by: jmarx
1 Replies

6. Shell Programming and Scripting

SED - delete words between two possible words

Hi all, I want to make an script using sed that removes everything between 'begin' (including the line that has it) and 'end1' or 'end2', not removing this line. Let me paste an 2 examples: anything before any string begin few lines of content end1 anything after anything before any... (4 Replies)
Discussion started by: meuser
4 Replies

7. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

8. Shell Programming and Scripting

Scramble a string.

Hi all, I am trying to write a perl script that will take user input as a string and scramble the string and print the result. Note: I cannot use shuffle function ....using for loop. so here is example. Enter a String: abcdef Print the Result: debacf Enter a String: abcdef print... (3 Replies)
Discussion started by: email-lalit
3 Replies
Login or Register to Ask a Question