Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Randomize columns in CSV file

Shell Programming and Scripting


Tags
csv, randomize

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-12-2017
eldeingles eldeingles is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 13 June 2017, 4:46 AM EDT
Location: Spain
Posts: 33
Thanks: 21
Thanked 0 Times in 0 Posts
Randomize columns in CSV file

Hi there, friends!

Writing exams again! This time my wish would be to randomize certain columns in a csv file. Given a file containing records consisting of 3 columns tab-separated:

A B C
A B C
A B C

I would love to get the columns of each record in random order...separated by a tab as well.

C B A
B C A
A C B
...

Love you all out there!


PS: This is not homework, I am an English teacher rewriting tests for my students.

Last edited by eldeingles; 06-12-2017 at 04:20 PM..
Sponsored Links
    #2  
Old Unix and Linux 06-12-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 16 August 2017, 3:57 PM EDT
Location: Aachen, Germany
Posts: 11,156
Thanks: 287
Thanked 3,439 Times in 3,168 Posts
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
Sponsored Links
    #3  
Old Unix and Linux 06-12-2017
eldeingles eldeingles is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 13 June 2017, 4:46 AM EDT
Location: Spain
Posts: 33
Thanks: 21
Thanked 0 Times in 0 Posts
Sorry, but...

I promise it is not homework...on the contrary, I am a teacher.
    #4  
Old Unix and Linux 06-12-2017
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 16 August 2017, 9:48 PM EDT
Location: Houston, Texas, USA
Posts: 950
Thanks: 0
Thanked 322 Times in 306 Posts

Code:
awk -v rn=$RANDOM '
BEGIN {
   srand(rn);
}
{
 f=NF;
 for (i=1 ; i<=f; i++) a[i]=i;
 n=1;
 while (n<4) {
    d=int((rand() * f+1));
    d=int((rand() * f+1));
    if (d > 0 && d < f+1) {
       if (d in a && a[d]) {a1[n]=$d; delete a[d]; n++;}
    }
 }
 $0="";
 for (i=1; i<(f+1); i++) $0=$0 OFS a1[i];
 $1=$1;
 print;
 for (i in a1) delete a1[i];
 for (i in a) delete a[i];
}

' OFS="\t" infile

The Following User Says Thank You to rdrtx1 For This Useful Post:
eldeingles (06-13-2017)
Sponsored Links
    #5  
Old Unix and Linux 06-12-2017
eldeingles eldeingles is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 13 June 2017, 4:46 AM EDT
Location: Spain
Posts: 33
Thanks: 21
Thanked 0 Times in 0 Posts
Uff, thanks rdrtx1m but I am not sure how to use this code...i am such a newbie. ...still trying to figure it out...
Sponsored Links
    #6  
Old Unix and Linux 06-12-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 16 August 2017, 3:57 PM EDT
Location: Aachen, Germany
Posts: 11,156
Thanks: 287
Thanked 3,439 Times in 3,168 Posts
Try also

Code:
while read TMP; do echo $(shuf -e $TMP) | tr ' ' '\t' ; done <file
C    B    A
A    C    B
A    B    C

The Following User Says Thank You to RudiC For This Useful Post:
eldeingles (06-13-2017)
Sponsored Links
    #7  
Old Unix and Linux 06-13-2017
eldeingles eldeingles is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 13 June 2017, 4:46 AM EDT
Location: Spain
Posts: 33
Thanks: 21
Thanked 0 Times in 0 Posts
My mistake

Yep, both scripts work...as log as the options to be shuffled are single letters/words (eg. A, B...just, never, ever...) but, what about if the options are groups or words?

Option A Option B Option C
I was I have been I will be
She's done She'll do She'd do
... ... ...

...and in case I had 4 options?

Thank you so much to you mates. Linux
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Randomize a file kylle345 Shell Programming and Scripting 3 05-23-2013 03:07 AM
Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks ks_reddy Shell Programming and Scripting 4 02-04-2013 04:37 AM
Randomize a matrix @man Shell Programming and Scripting 5 12-13-2012 03:23 AM
Randomize letters jeppe83 Shell Programming and Scripting 21 06-20-2012 08:19 PM



All times are GMT -4. The time now is 10:01 PM.