Randomize letters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Randomize letters
# 1  
Old 06-18-2012
Randomize letters

Hi,

Is there a tool somewhat parallel to rev, but which randomizes instead of reverses?

I've tried rl, but I can only get it to randomize words.

I was hoping for something like this
Code:
echo "hello" | ran 
leolh

less simpler solutions are also welcome.
Sorry if the question is trivial or answered before, but I cant seem to find a solution..
# 2  
Old 06-18-2012
Hi.

Code:
echo hello | fold -w 1 | sort -r | tr -d '\n';echo

producing
Code:
ollhe

See man pages for details.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 3  
Old 06-18-2012
Thank you. Just what I was looking for!

I replaced sort -r with shuf, so I could use it for iteration.

Code:
jeppe@space:/$ while :;do echo "palindrom" | fold -w 1 | shuf | tr -d '\n';echo;done
daipmonrl
rlmipdaon
mpriladon
aiomrplnd
lnpraiomd
nidlpraom
ilmpadnro
rodnlamip
odapnimrl
riodlmanp
oadpimrnl
...etc.

# 4  
Old 06-19-2012
Quote:
Originally Posted by drl
Hi.

Code:
echo hello | fold -w 1 | sort -r | tr -d '\n';echo

producing
Code:
ollhe

See man pages for details.

Best wishes ... cheers, drl
Hold on, there is nothing random about sort -r, obviously Smilie
--
Edit: Ah I see you probably mean (GNU) sort -R

Last edited by Scrutinizer; 06-19-2012 at 02:21 AM..
# 5  
Old 06-19-2012
Here's an implementation of Fisher Yates Shuffle algorithm (from perlfaq4):

Code:
#! /usr/bin/perl -w
use strict;

my $string = "hello";
my @x = split '', $string;
fisher_yates_shuffle( \@x );
print @x;

sub fisher_yates_shuffle {
    my $deck = shift;
    return unless @$deck;
    my $i = @$deck;
    while (--$i) {
        my $j = int rand ($i+1);
        @$deck[$i,$j] = @$deck[$j,$i];
    }
}

# 6  
Old 06-19-2012
It seems sort -R works faster than shuf..

I'm experimenting with a script that finds anagrams.

So far this does the job:

Code:
#!/bin/bash
#anagramfinder
while :
do
WORD=$(shuf -n 1 /data/korpus2k/kord)
AG=$(echo "$WORD" | fold -w 1 | sort -R | tr -d '\n';echo)
CHECK=$(grep -w "$AG" /data/korpus2k/kord | wc -l)
	if [ "$CHECK" -eq "0" ]
	then true
	elif [ "$WORD" = "$AG" ]
	then true	
	else  
	echo "$WORD" "$AG"			
	fi
done

The file kord is a word list containing 162060 meaningful Danish words, 1 pr. line.

Is there a way for the script to work on the first word of kord, randomize the letters until it finds itself or another, then the next etc.. instead of picking random words in 'eternal' iteration?

Last edited by jeppe83; 06-19-2012 at 09:57 AM..
# 7  
Old 06-19-2012
Why randomize at all? You could scan kord, eliminating each word in turn if it is not the same length or does not use the same letters with the same frequency. 162,060 isn't a lot to brute.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Random letters

Hi there, first of all this is not homework...this is a new type of exercise for practicing vocabulary with my students. I have a file consisting of two columns, separated by a tab, each line consisting of a word and its definition, separated by a line break. What i need is to replace a... (15 Replies)
Discussion started by: eldeingles
15 Replies

2. Shell Programming and Scripting

Randomize columns in CSV file

Hi there, friends! Writing exams again! This time my wish would be to randomize certain columns in a csv file. Given a file containing records consisting of 3 columns tab-separated: A B C A B C A B C I would love to get the columns of each record in random order...separated by a tab as... (12 Replies)
Discussion started by: eldeingles
12 Replies

3. UNIX for Dummies Questions & Answers

How to cut only letters?

I was wondering how I could cut only the names of items from the following list: spoons50 cups29 forks50 plates29 I used "man cut" and thought -c would help, but the items have different character lengths. Please note that there is no space between the item and number (so I can't use... (10 Replies)
Discussion started by: PTcharger
10 Replies

4. Shell Programming and Scripting

Randomize a file

Hi, I have a large file that looks like this: @FCC189PACXX:2:1101:1420:2139/1 AGCGAGACTCCGTCTCAAAAAGAAAAAATTTTTCAAAATATTGCAATGGGCTTGTAATTTCTGCTTAAATGTCAGGAGGTCTGAGCCATT + bbbeeeceggggghiiiiiiiiiihfihihiiihhhghiihhihifhihiihhhhhhhhiiigfggggdceeeeebdcc^``bbcbccbb... (3 Replies)
Discussion started by: kylle345
3 Replies

5. Shell Programming and Scripting

Randomize a matrix

--please have a look at my third post in this thread! there I explained it more clearly-- Hey guys. I posted a complex problem few days back. No reply! :| Here is simplified question: I have a matrix with 0/1: * col1 col2 col3 row1 1 0 1 row2 0 0 ... (5 Replies)
Discussion started by: @man
5 Replies

6. UNIX for Advanced & Expert Users

Add letters

I want to add letters A,B,C,… in front of every line of input while printing them out using PERL. eg A file is parsed as a cmd line arg and its context will be displayed as A line1... B line 2.. I tried this..but I want better and perfect solution! !perl -p my $counter; BEGIN { $counter... (4 Replies)
Discussion started by: aadi_uni
4 Replies

7. Shell Programming and Scripting

Need to strip few letters

Hey guys.. Can experts help me in achieving my purpose.. I have a file which contains email address of some 100 to 1000 domains, I need only the domain names.. Eg: abc@yahoo.com hd@gamil.com ed@hotmail.com The output should contain only Yahoo.com ... (5 Replies)
Discussion started by: achararun
5 Replies

8. Shell Programming and Scripting

trim letters

Hello, I have a list of words.. ranging from 4 to any characters long.. to not more than 20 though. How can I select only first seven letters of the list of words? example:- wwwwwwwwww eeeee wererreetf sdsarddrereewtewt sdsdsds sdsd ereetetttt ewtwertwrttrttrtrtwtrww I... (10 Replies)
Discussion started by: fed.linuxgossip
10 Replies

9. UNIX for Dummies Questions & Answers

capital letters GONE!

I have an odd issue. I am trying to copy some files/folders to my linux box via a burned CD which I created on my mac. When I browse the files on the mac (or my windows box), everything looks fine (some of the folder names start with a capital letter, which is needed for everything to work... (8 Replies)
Discussion started by: blogg
8 Replies
Login or Register to Ask a Question