Randomize columns in CSV file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Randomize columns in CSV file
# 8  
Old 06-13-2017
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
This User Gave Thanks to RudiC For This Post:
# 9  
Old 06-13-2017
Magic!!

It worked!! It worked!! Yep! Smilie

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!Smilie

---------- 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?
# 10  
Old 06-13-2017
I guess so, as long as word seperators and field sep. are clearly distinguished. Why don't you give it a shot?
# 11  
Old 06-13-2017
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..
# 12  
Old 06-13-2017
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

# 13  
Old 06-13-2017
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match columns from two csv files and update field in one of the csv file

Hi, I have a file of csv data, which looks like this: file1: 1AA,LGV_PONCEY_LES_ATHEE,1,\N,1,00020460E1,0,\N,\N,\N,\N,2,00.22335321,0.00466628 2BB,LES_POUGES_ASF,\N,200,200,00006298G1,0,\N,\N,\N,\N,1,00.30887539,0.00050312... (10 Replies)
Discussion started by: djoseph
10 Replies

2. Shell Programming and Scripting

Randomize a file

Hi, I have a large file that looks like this: @FCC189PACXX:2:1101:1420:2139/1 AGCGAGACTCCGTCTCAAAAAGAAAAAATTTTTCAAAATATTGCAATGGGCTTGTAATTTCTGCTTAAATGTCAGGAGGTCTGAGCCATT + bbbeeeceggggghiiiiiiiiiihfihihiiihhhghiihhihifhihiihhhhhhhhiiigfggggdceeeeebdcc^``bbcbccbb... (3 Replies)
Discussion started by: kylle345
3 Replies

3. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

4. Shell Programming and Scripting

Checking the CSV File Columns

Hi All, i have a CSV file like below, col1 col2 col3 col4 col5 col6 col7 a1 a2 a3 a4 a5 a6 a7 b1 b2 b3 b4 b5 b6 b7 c1 c2 c3 c4 c5 c6 c7 d1 d2 d3 d4 d5 d6 d7 Col1,col2.. are the column names and... (1 Reply)
Discussion started by: dileep_d10
1 Replies

5. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

6. Shell Programming and Scripting

Keep only certain columns in a csv file

I have a file that has 500+ columns but only need 15 of those and all the other ones can be removed. What is the best way to do this? Can I get some examples please? (3 Replies)
Discussion started by: GroveTuckey
3 Replies

7. UNIX for Advanced & Expert Users

Unix Bash: substitute columns in .csv using other .csv columns

Hi All, I have two .csv's input.csv having values as (7 columns) ABC,A19907103,ABC DEV YUNG,2.17,1000,2157,07/07/2006 XYZ,H00213850,MM TRUP HILL,38.38,580,23308,31/08/2010 output.csv having (25 columns) A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y... (4 Replies)
Discussion started by: abhivyas
4 Replies

8. Shell Programming and Scripting

Deleting columns from CSV file

Hi All, Am working on perl script which should delete columns in existing CSV file. If my file is : AA,BB,CC,DD 00,11,22,33 00,55,66,77 00,99,88,21 AA,BB... are all my headers can come in any order (e.g AA,CC,BB...) and rest are values. I want to delete column CC... Can anybody help... (2 Replies)
Discussion started by: darshakraut
2 Replies

9. Shell Programming and Scripting

no of columns in csv file

Hi, I would like to know how can I find the number of columns in each line in a csv file. I have a csv file wiht comma separated an dhas 10000 line in it. I want to verify the number of columns in each line. is there any way to find it out using awk or any other commands? Thanks.... (2 Replies)
Discussion started by: McLan
2 Replies

10. Shell Programming and Scripting

Help with comparing columns from a csv file

Hi there, I have an csv file. I want to compare the 16th and 18th columns. They contain alpha numeric characters. Some are same and some are different. We have to pick the ones which are different. But with certain rules. 16th col. 18th col. ---------- ... (1 Reply)
Discussion started by: sickboy
1 Replies
Login or Register to Ask a Question