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
    #8  
Old Unix and Linux 06-13-2017   -   Original Discussion by eldeingles
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 November 2017, 1:17 PM EST
Location: Aachen, Germany
Posts: 11,652
Thanks: 323
Thanked 3,620 Times in 3,325 Posts
The field / group separator must be different from the word separator, e.g. spaces between words, <TAB>s between groups. Seems to be easier in awk than in pure shell, but try (recent shell, e.g. bash 4 required)

Code:
while IFS="    " read -a TMP; do shuf -i0-$((${#TMP[@]}-1)) | while IFS="    " read LN; do printf "%s\t" "${TMP[$LN]}"; done; printf "\n"; done <file
B    D    A    C    
B    E    A    C    D    
B    C    A    
Option B    Option C    Option A    
I have been    I was    I will be    
She will have done    She'll do    She'd do    She's done

Watch out, the IFS is being set to a <TAB> char.

Last edited by RudiC; 06-13-2017 at 05:01 AM.. Reason: typo
The Following User Says Thank You to RudiC For This Useful Post:
eldeingles (06-13-2017)
Sponsored Links
    #9  
Old Unix and Linux 06-13-2017   -   Original Discussion by eldeingles
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
Magic!!

It worked!! It worked!! Yep! Linux

A long story as I am working on a Mac. First you have to install HOMEBREW brew install coreutils, change the order shuf to gshuf.
Also, you have to update your BASH version to 4 (I followed this link Upgrade to bash 4 in Mac OS X – clubmate.fi, then go on scripting.

Great job!...and done quickly! Super!Linux

---------- Post updated at 02:55 AM ---------- Previous update was at 02:53 AM ----------

As I see it it will shuffle any number of columns, won't it?
Sponsored Links
    #10  
Old Unix and Linux 06-13-2017   -   Original Discussion by eldeingles
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 November 2017, 1:17 PM EST
Location: Aachen, Germany
Posts: 11,652
Thanks: 323
Thanked 3,620 Times in 3,325 Posts
I guess so, as long as word seperators and field sep. are clearly distinguished. Why don't you give it a shot?
    #11  
Old Unix and Linux 06-13-2017   -   Original Discussion by eldeingles
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
Yes!

file contents:

you / you do / do you / you did
she got / did she get / did she got / she gotten
are makeing / making / are making / is making
usually have / has usually / usually has / have usually
$ ./random

result:

you / you do / do you / you did
did she got / she gotten / she got / did she get
are making / are making / is making / making
usually has / usually have / has usually / have usually
That's it!

Last edited by eldeingles; 06-13-2017 at 05:10 AM..
Sponsored Links
    #12  
Old Unix and Linux 06-13-2017   -   Original Discussion by eldeingles
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 November 2017, 1:17 PM EST
Location: Aachen, Germany
Posts: 11,652
Thanks: 323
Thanked 3,620 Times in 3,325 Posts
The result doesn't look too good - is that an artifact that happened when dragging / dropping the text?

This is how it looks like if I run it on my system:

Code:
you do	you	do you	you	
she gotten	did she got	did she get	she got	
are makeing	is making	are making	making	
usually has	has usually	have usually	usually have

Sponsored Links
    #13  
Old Unix and Linux 06-13-2017   -   Original Discussion by eldeingles
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
input:


Code:
you    you do    do you    you did
she got    did she get    did she got    she gotten
are makeing    making    are making    is making
usually have    has usually    usually has    have usually

output:

Code:
you do    do you    you did    you    
she gotten    did she got    she got    did she get    
are making    making    are makeing    is making    
usually has    usually have    has usually    have usually


Last edited by RudiC; 06-13-2017 at 05:58 AM..
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 04: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 05:37 AM
Randomize a matrix @man Shell Programming and Scripting 5 12-13-2012 04:23 AM
Randomize letters jeppe83 Shell Programming and Scripting 21 06-20-2012 09:19 PM



All times are GMT -4. The time now is 02:37 PM.