LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 12:50 AM   #1
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Rep: Reputation: 0
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".
 
Old 10-10-2010, 02:16 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Code:
echo "test1,test34,test5,test6" | tr ',' "\n"|shuf|tr "\n" ","
 
Old 10-10-2010, 03:37 AM   #3
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
Shuf Command is not finding in shell.Is there any other way to achieve this.
 
Old 10-10-2010, 03:41 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Well, you can also do:

Code:
echo "test1,test2,test3,test4" | tr , "\n" | sort -R | tr "\n" ,
 
Old 10-10-2010, 03:51 AM   #5
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
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"
 
Old 10-10-2010, 04:01 AM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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
 
Old 10-10-2010, 04:09 AM   #7
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
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

Last edited by mariakumar; 10-10-2010 at 04:27 AM.
 
Old 10-10-2010, 04:47 AM   #8
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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.
 
Old 10-10-2010, 04:57 AM   #9
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS
Posts: 177

Rep: Reputation: 26
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
 
Old 10-10-2010, 05:16 AM   #10
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
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
 
Old 10-10-2010, 05:19 AM   #11
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS
Posts: 177

Rep: Reputation: 26
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/,$//')
 
Old 10-10-2010, 05:40 AM   #12
hussain.s
LQ Newbie
 
Registered: Oct 2010
Posts: 9

Rep: Reputation: 0
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
 
Old 10-10-2010, 05:49 AM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 10-11-2010, 12:32 AM   #14
mariakumar
LQ Newbie
 
Registered: Oct 2010
Location: India
Distribution: Mac OS
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks mario.almeida your solution..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help, converting date format in a comma separated field! mgsmith7475 Programming 2 04-09-2010 11:34 AM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 10:23 AM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM


All times are GMT -5. The time now is 08:02 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration