shuffle pack of words in line

hello
i just seeking for a simple way to make a shuffle by block of words in a line. no matter shell (sh/bash) or perl

should be like this:
the message (which is line of some file) splits to packs (packs are random 5-10 words in each) then making a new line inserting those packs in a random order. then goes to next line in file
simple as that :slight_smile:

This requirement does not seem to bear any significance in the real world.

Is this a homework question?

no it's a real task
i'm just feeding antispam bot with tons of text

What does the input file look like?

What should the output look like?

example:
input file is full of lines such like this:
the message (which is line of some file) splits to packs (packs are random 5-10 words in each) then making a new line inserting those packs in a random order. then goes to next line in file

output file should be:
making a new line inserting those order. then goes to next line in file splits to packs (packs are random the message (which is line of some file) 5-10 words in each) then packs in a random

then next line in those file is another line of words

---------- Post updated at 10:40 AM ---------- Previous update was at 10:38 AM ----------

it should be pretty easy on perl with hes regexp and arrays
but should be not hard with awk too. maybe 1 line even
atm i'm trying to make this script on shell by making array and then take 5-10 random array elements and make a new message but this will be alot of 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
}

I can think of at least one important site who would have a fit at this thread.

The Spamhaus Project

Hopefully the attempt to crack the spam filters will result in a block.

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!

Please put code inside

 tags.


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

[/quote]

[indent]
UUOC. And the braces are unnecessary.

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

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

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.

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:

#!/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