random words


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers random words
# 1  
Old 02-27-2011
random words

Hi there folks,

for an exercise for my pupils (you know i am always thinking of them!) i need to randomly re-arrange the words (blank space separated) in a sentence (a line in a textfile). Any inspiration??

Txk so much.
# 2  
Old 02-27-2011
Try:
Code:
perl -ane 'use List::Util 'shuffle'; $,=" ";print shuffle(@F);' file

# 3  
Old 02-27-2011
Code:
awk 'BEGIN { srand() }
{
  split(x, s)
  j = int(NF * rand() + 1)
  for (i = 0; ++i <= NF;) {
    while (j in s) j = int(NF * rand() + 1)
    printf "%s", ($j (i < NF ? FS : RS)); s[j]
    }
  }' infile

This User Gave Thanks to radoulov For This Post:
# 4  
Old 02-27-2011
Code:
$ ruby -ne 'BEGIN{srand};puts $_.split.shuffle.join(" ")' file
for test is shuffle a random this
$  ruby -ne 'BEGIN{srand};puts $_.split.shuffle.join(" ")' file
for random is shuffle a this test
$ ruby -ne 'BEGIN{srand};puts $_.split.shuffle.join(" ")' file
is for this shuffle test random a

This User Gave Thanks to kurumi For This Post:
# 5  
Old 02-28-2011
wow, that's teamwork

After a few attempts this is what i got:

bartus11: worked but lines were not separated by a return. Although i could shuffle a list of words separated by a tab (\t)

radoulov: couldnt get the script to work (remember i am a newbie)

kurumi: the best! nailed it! even though i tried to apply it to a list of words separated by a tab (\t) but didn't work.

My other job was to shuffle the words in a tab-separated list like this:

go went gone
sleep slept slept
...

to get something like:

went go gone
slept slept sleep
# 6  
Old 02-28-2011
Quote:
Originally Posted by eldeingles

kurumi: the best! nailed it! even though i tried to apply it to a list of words separated by a tab (\t) but didn't work.

My other job was to shuffle the words in a tab-separated list like this:

go went gone
sleep slept slept
...

to get something like:

went go gone
slept slept sleep
it should work.

Code:
$ od -c file #notice the tabs
0000000   g   o  \t   w   e   n   t  \t   g   o   n   e  \n   s   l   e
0000020   e   p  \t   s   l   e   p   t  \t   s   l   e   p   t  \n
0000037

$ ruby -ne 'BEGIN{srand};puts $_.split.shuffle.join(" ")' file
went gone go
slept sleep slept

# 7  
Old 02-28-2011
Quote:
Originally Posted by eldeingles
After a few attempts this is what i got:

bartus11: worked but lines were not separated by a return. Although i could shuffle a list of words separated by a tab (\t)
Try:
Code:
perl -lane 'use List::Util 'shuffle'; $,=" ";print shuffle(@F);' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create 'n' number random pairwise combination of words

File 1 contains the list of words that needed to be randomly paired: Tiger Cat Fish Frog Dog Mouse Elephant Monkey File 2 contains the pairs that should not be used (in any solution) during random pairing. Elephant-Dog Cat-Fish Monkey-Frog Dog-Elephant, Fish-Cat, Frog-Monkey... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. 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

3. 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

4. UNIX for Dummies Questions & Answers

Deleting words between every appearance of two words

Hi there, newbie there. I've been browsing the forums hoping to find a solution that answers a problem similar to what I need, but haven't had much luck. Any help would be greatly appreciated. Thanks! I need to delete a bunch of text between every appearance of two words in a really large file... (3 Replies)
Discussion started by: lendl
3 Replies

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

9. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

10. 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
Login or Register to Ask a Question