LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-10-2010, 04:02 AM   #1
hussain.s
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Rep: Reputation: 0
Randomly shuffling of array elements


hi
i have an array var=(node1,node2,node3,node4) and i want to shuffle the array elements randomly every time when unix script will run like that.....


first time when script run order will be shuffle randomly
node3,node1,node4,node2

second time when script run order will be shuffle randomly like
node1,node4,node2,node3


third time when script will run again order will be shuffle randomly like
node2,node1,node3,node4


how to do this without using of shuf or shufsequence commands

please do the needful
 
Old 10-10-2010, 05:23 AM   #2
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS, Manjaro
Posts: 179

Rep: Reputation: 27
Hi,

May be this can help you.

http://www.linuxquestions.org/questi...2/#post4122764
 
Old 10-10-2010, 06:02 AM   #3
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS, Manjaro
Posts: 179

Rep: Reputation: 27
Well,

First of all you need to update your own post.

Up to my knowledge I don't think I can do that with bash.

declare -a temp
temp=($(echo node1,node2,node3,node4 | tr ',' '\n' | awk 'BEGIN { srand() } { print rand() "\t" $0 }' | sort -n | cut -f2- ))

above temp will hold it as an array
 
Old 10-11-2010, 12:33 AM   #4
hussain.s
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
hi,
thanks for your solution but is it possible that we can do the randomly shuffling of array elements without using of awk file
 
Old 10-11-2010, 01:16 AM   #5
hussain.s
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
hi,
the below is my code
in this code my A2 file have some values like test1,test2,test3 etc and in the temp variable iam randomly shuffling the order of A2 file elements each time when running the script
like one time when run the script it shuffle the order randomly
test2,test3,test1
second time when script run again it shuffle the order randomly
test3,test1,test2
like that each time when script is running it shuffle the order randomly

now i want to pass the temp variable which have A2 file value to the another file A3 iam able to pass the value but it is always showing the same order when every time iam running the script but i want that each time when script will run value of file A3 should be changed randomly when A2 file value should be shuffled randomly because indirectly iam passing the A2 file value to the A3 file

please do the neeful


#!/bin/sh
APP_INSTANCE_ID=56
file1=/Users/hussain/Desktop/shells/A2 //here A2 is the text file which have value test1,test2,test3
file2=/Users/hussain/Desktop/shells/A3 ////A3 is the text file in which A2 file value is coming
id=`cat $file1 | cut -d "=" -f 2`


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

echo "$temp"

sed "s/$temp//g" $temp > $l_tmp_web_config_file2 //in this line value of A2 is coming in the A3 file but it is
always showing the same order each time when script is running
 
Old 10-11-2010, 09:32 AM   #6
hussain.s
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Original Poster
Rep: Reputation: 0
hi,
this is my file1 which have below contents
test=test1,test2,test3,test4,test5
hi
hello
world

now i want to display the contents of file1 into another file2 here file1 contents of first line is shuffling randomly i want the output like that each time when script will run
first time when script run
test=test2,test1,test3,test4,test5//here value is randomly shuffling
hi
hello
world


second time when script will run
test=test4,test3,test2,test1,test5//here value is again randomly shuffling
hi
hello
world

but when iam running the script output is comming like that
test=test4,test3,test2,test1,test5,hi,hello,world
is there any solution
 
Old 10-11-2010, 10:14 AM   #7
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS, Manjaro
Posts: 179

Rep: Reputation: 27
Hi,

May be you can try something like this.

Save the below output to a file format.sh
Code:
#!/bin/bash

file="$1"
[[ ! -f "$file" ]] && exit 0
declare -i lineCount=$(wc -l < $file)

oldRandNo=$(( $lineCount+2 ))

for (( i=1; i<=$lineCount; i++)); do
        declare -a wc_array=($(sed -n "${i}p" $file | tr ',' ' '))

        wc_count=${#wc_array[@]}

        if [[ $wc_count -gt 0 ]]; then
                randNo=$((RANDOM % $wc_count))
                if [[ $randNo -eq $oldRandNo ]]; then
                        if [[ $randNo -eq 0 ]]; then
                                randNo=$(( $wc_count-1 ))
                        else
                                randNo=$((RANDOM % $wc_count))
                                if [[ $randNo -eq $(( wc_count-1 )) ]]; then
                                        randNo=0
                                else
                                        randNo=$(( randNo+1 ))
                                fi
                        fi
                fi
        else
                continue
        fi


        string="${wc_array[$randNo]} "

        for (( y=0; y<$wc_count; y++ )); do
                [[ $y -ne $randNo ]] && string+="${wc_array[$y]} "
        done
        echo $string | tr -s '[:blank:]' | sed 's/ /,/g'
        oldRandNo=$randNo
done
Quote:
sh format.sh file1
 
Old 10-11-2010, 11:03 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
hussain.s, please use [code][/code] around your code, to preserve formatting and to improve readability.

Here's a bash-builtin only way to shuffle array elements. I wouldn't try it on very large arrays though, as it uses a simple brute-force testing loop to keep it from creating duplicates. I used the comma-separated input list given above.
Code:
IFS=","
test="test1,test2,test3,test4,test5"
array1=( $test )

num=${#array1[@]}
complete=""

for i in  ${!array1[@]}; do
     while true; do
          newi=$(( RANDOM%num ))

          if [[ ! $complete =~ "$newi:" ]]; then
               array2[$newi]="${array1[$i]}"
               complete="${complete}${newi}:"
               break
          fi
     done
done

echo "${array2[*]}"
# [#] here will the entire array as a single string, using the current IFS as separator.  Commas in this case.  Must be quoted.
# [@] will print each element individually separated by spaces.
There are probably better ways to do it.

Last edited by David the H.; 10-11-2010 at 11:07 AM. Reason: fixed minor error
 
Old 10-11-2010, 02:47 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I just discovered that there's a gnu coreutils application called shuf that can be used to randomize an input sequence. Here's a greatly simplified version of my last script that uses it.
Code:
teststring="test1,test2,test3,test4,test5,test6"
array1=( ${teststring//,/ } )

num=$(( ${#array1[@]} -1 ))
newseq=$( shuf -i 0-$num )

for i in $newseq; do

     outstring+="${array1[$i]},"

done

echo ${outstring%,}
Much cleaner overall, although it does now depend on an external tool. But at least that tool is a core utility. You'll notice I've also eliminated messing around with the IFS.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
perl array last 2 elements will not pop casperdaghost Programming 3 04-27-2010 10:45 PM
c++ array question on non assigned elements davimint Programming 9 11-24-2008 09:43 AM
print array elements in one line bharatbsharma Programming 1 10-29-2007 08:58 AM
odd behaviour of array elements in c++ markhod Programming 4 03-14-2005 09:58 AM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration