LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Randomize the comma separated string in shell (https://www.linuxquestions.org/questions/linux-general-1/randomize-the-comma-separated-string-in-shell-837172/)

mariakumar 10-10-2010 12:50 AM

Randomize the comma separated string in shell
 
I am newbie to Linux.I wanted to randomize the comma separated string.
String like "test1,test34,test5,test6".And it would be displayed differently when I ran each time.

Expected output like "test5,test6,test1,test34".

ghostdog74 10-10-2010 02:16 AM

Code:

echo "test1,test34,test5,test6" | tr ',' "\n"|shuf|tr "\n" ","

mariakumar 10-10-2010 03:37 AM

Shuf Command is not finding in shell.Is there any other way to achieve this.

H_TeXMeX_H 10-10-2010 03:41 AM

Well, you can also do:

Code:

echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,

mariakumar 10-10-2010 03:51 AM

thank you for this solution
But i want like that each time when we run the script the order of words should be shuffled randomly.
one time we run the script the order will be ===> "test1,test3,test4"
another time when script will run again order should be changed like ==> "test3,test4,test1"

H_TeXMeX_H 10-10-2010 04:01 AM

Code:

bash-4.1$ echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,
test1,test3,test2,test4,bash-4.1$ echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,
test4,test2,test3,test1,bash-4.1$ echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,
test3,test2,test4,test1,bash-4.1$ echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,
test2,test1,test3,test4,bash-4.1$ echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,
test3,test2,test4,test1,bash-4.1$ echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,
test4,test3,test1,test2,bash-4.1$ echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,

sort -R = random sort

mariakumar 10-10-2010 04:09 AM

hi,
sort -R command is not found due to which it is showing invalid -R command
is there any other way for doing the shuffling randomly

H_TeXMeX_H 10-10-2010 04:47 AM

Not that I know of. You don't have shuf, you don't have sort ... can't think of anything else, except doing it manually.

mario.almeida 10-10-2010 04:57 AM

Hi,

This is a bit longer one.

echo test1,test2,test3,test4,test5 | tr ',' '\n' | awk 'BEGIN { srand() } { print rand() "\t" $0 }' | sort -n | cut -f2- | tr '\n' ','; echo

mariakumar 10-10-2010 05:16 AM

Thanks for your solution
but one problem is coming after running the script, it is appending comma in last word
test1,test2,test3,test4,test5,
how to remove this comma in last word and how to assign the output in some other variable like


tmp=echo test1,test2,test3,test4,test5 | tr ',' '\n' | awk 'BEGIN { srand() } { print rand() "\t" $0 }' | sort -n | cut -f2- | tr '\n' ','; echo

mario.almeida 10-10-2010 05:19 AM

To remove , you can add sed 's/,$//'

temp=$(echo test1,test2,test3,test4,test5 | tr ',' '\n' | awk 'BEGIN { srand() } { print rand() "\t" $0 }' | sort -n | cut -f2- | tr '\n' ',' | sed 's/,$//')

hussain.s 10-10-2010 05:40 AM

hi,
this looks fine but it is not working in array it is giving the same order it is not showing the random order of array elements but is there any way to randomly shuffle the array elements directly without converting it as string

ghostdog74 10-10-2010 05:49 AM

Code:

BEGIN{srand();FS="," }
{
  for(i=1;i<=NF;i++){
    a[++d]=$i
  }
 }
END{
    while (1){
        if (e==d) {break}
        RANDOM = int(1 + rand() * d)
        if ( RANDOM in a  ){
                print a[RANDOM]
                delete a[RANDOM]
                ++e
                }
        }
}

Code:

$ echo test1,test2,test3,test4,test5| awk -f test.awk
test4
test3
test2
test1
test5


mariakumar 10-11-2010 12:32 AM

Thanks mario.almeida your solution..


All times are GMT -5. The time now is 11:48 PM.