shuffle pack of words in line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shuffle pack of words in line
# 8  
Old 09-28-2009
Quote:
Originally Posted by cfajohnson

Code:
randomize()
{
  set -f
  set -- $1
  set +f
  while [ $# -ge 5 ]
  do
    pack+=( "$1 $2 $3 $4 $5" )
    shift 5
  done

  if [ $# -gt 0 ]
  then
    pack+=( "$*" )
  fi

  while [ ${#pack[@]} -gt 0 ]
  do
    n=$(( $RANDOM % ${#pack[@]} ))
    printf "%s " "${pack[$n]}"
    unset pack[$n]
    pack=( "${pack[@]}" )
  done
  echo
}

yeah, working like that
is it works only with command line?
cuz i dont know how to get line from file and make it vars $1 $2 etc which is command line vars only
is there a way to make it work with reading lines from files?

P.S. what exactly means
set -f
set -- $1
set +f
?

why used # in a:

while [ ${#pack[@]} -gt 0 ]

i know $(pack[@]} shows whole array but u using random on it when it contain words not numbers so u need to use # or why? looks like it works like $# means gives u only number of arrays

and why pack+ goes with +

---------- Post updated at 09:01 PM ---------- Previous update was at 08:55 PM ----------

oh i got it

cat somefile |
{
while read linecho; do
randomize "$linecho"; echo ""
done
}



groovy. thanks!

Last edited by tip78; 09-28-2009 at 02:06 PM..
tip78
# 9  
Old 09-28-2009
Quote:
Originally Posted by tip78

Please put code inside [code] tags.
Quote:
Code:
cat somefile |
{
while read linecho; do
randomize "$linecho"; echo ""
done
}


UUOC. And the braces are unnecessary.

Code:
while IFS= read -r linecho; do
   randomize "$linecho"; echo ""
done < somefile



---------- Post updated at 01:19 PM ---------- Previous update was at 01:10 PM ----------

Quote:
Originally Posted by tip78
P.S. what exactly means
set -f
set -- $1
set +f
?

why used # in a:

while [ ${#pack[@]} -gt 0 ]

i know $(pack[@]} shows whole array but u using random on it when it contain words not numbers so u need to use # or why? looks like it works like $# means gives u only number of arrays

and why pack+ goes with +[COLOR="#738fbf"]

The answers are in the bash man page.

Please read the The UNIX and Linux Forums rules:
(9) ... (don't write in cyberchat or cyberpunk style). English only.

"u" is not English.
# 10  
Old 10-21-2009
allrighty
now i know perl enuf to make same task even faster and more effective (didnt know perl at all when started this thread)
here we go:

Code:
#!/usr/bin/perl

use warnings;
use locale;

my$curfile=$ARGV[0] or die"no arguments! launch format: $0 srcfile\ntry again.\n";
my$endfile="shuffled";

print "shuffling blocks of 5 words in every line for \"$curfile\"..\n";

open(FH,"$curfile")or die"cant open $curfile: $!\n";
open(TMP,">tmp");
while($line=<FH>){$line=~s/\n/ /;
    while($line=~/^(.+? ){5}/){
    $line=~s/^((.+? ){5})//;
    push(@arr,"$1");
    next;
    }
push(@arr,"$line");
    for($i=0;$i<@arr;$i++){
    (int rand(2)==0) ? (push(@arr2,$arr[$i])) : (unshift(@arr2,$arr[$i]))
    }
print TMP @arr2,"\n";undef@arr;undef@arr2;
}
close TMP;

open(FH,"tmp");open(NFH,">$endfile");
while(<FH>){s/\s+$/\n/;print NFH "$_"}
close FH;unlink("tmp");

print"finaly done! check \"$endfile\"\n";

works like 5 times faster. perl rulz

Last edited by tip78; 10-21-2009 at 07:29 AM..
tip78
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. UNIX for Beginners Questions & Answers

Logic shuffle

Hi, Is there any way I can shuffle the numbers randomly. I have been trying to google and I found lots of 'generator' but is it possible to find the background logic to create randomness? Thanks, (3 Replies)
Discussion started by: Indra2011
3 Replies

3. Shell Programming and Scripting

Randomized shuffle words on each line

Hi Folks, I have a text file with a thousand lines consisting of words or a group of words separated by commas. I would like to randomize / shuffle the words on each line. Eg; file.txt Linux,Open,Free,Awesome,Best Things in Life,The Greatest Laptop,PC,Tablet,Home Computers,Digital... (2 Replies)
Discussion started by: martinsmith
2 Replies

4. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

5. Shell Programming and Scripting

get few words from same line

Hello all i tried awk , cut but i think something missing i have this line @@XYMONDCHK-V1|.acklist.|developer_instead|rendy_google_yagom|1323977582|1323979382|1323979382|1|admin|test case i want cut some words to be in new line like this... (6 Replies)
Discussion started by: mogabr
6 Replies

6. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

7. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

8. Shell Programming and Scripting

print only last two words of a line

can u help me out to print last two words of each sentence of a file. for example. contents of input file: i love songs my favourite songs sent songs all kind good buddy Ouput file should contain: love songs favourite songs sent all kind good buddy (5 Replies)
Discussion started by: pradeepreddy
5 Replies

9. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies

10. Shell Programming and Scripting

seperating the words from a line??

The line is like this +abc+def+mgh+ddsdsd+sa i.e. words seperated by +. There is a plus in the beginning. i want to conver this line to abc, def, mgh, ddsdsd, sa please provide the logic in the form of a shell script Thanks in advance (3 Replies)
Discussion started by: skyineyes
3 Replies
Login or Register to Ask a Question