how to choose random columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to choose random columns
# 1  
Old 04-21-2011
Question how to choose random columns

Hello!

Can anybody suggest about the fastest way of extracting "n" random columns from a very large file (tab separated) having thousands of columns, where n can be any specified number.

Thanks!
# 2  
Old 04-21-2011
Just one line of thousands of columns?

If you have or can use the rl utility, it is quite fast indeed:

Code:
rl -d $'\t' -c 32 tabseperatedfile

These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 04-21-2011
You can use the "cut" command - the default delimiter is horizontal tab:
Code:
cut -fn Inp_File

Where "n" is the column number which can be of any length, including zero.
This User Gave Thanks to Shell_Life For This Post:
# 4  
Old 04-24-2011
Hey! may be I was not very clear. I wanted to extract 'n' random columns where 'n' can be 250 or 400 or any other number. And the file has thousands of columns (lets say 5000) and hundreds of rows (lets say 400).

Thanks!
# 5  
Old 04-24-2011
Actually it is not very clear

lets say you give n=100, you want to extract first 100 columns from all the rows.

regards,
Ahamed

---------- Post updated at 02:12 AM ---------- Previous update was at 02:06 AM ----------

To extract columns from m to n where n>m

Code:
#awk '{gsub("\t"," ",$0);print}' file | cut -d" " -fm-n
#eg:
awk '{gsub("\t"," ",$0);print}' file | cut -d" " -f3-5

#eg to extract first 400 columns
awk '{gsub("\t"," ",$0);print}' file | cut -d" " -f1-400

regards,
Ahamed
This User Gave Thanks to ahamed101 For This Post:
# 6  
Old 04-24-2011
Thanks for your reply ahamed but I need to extract random columns like if I say I want to extract 50 random columns it should extract it randomly from all the columns, lets say it extracts the column no. 10, 28, 40, 57, 92, 500, 740, 2540, ... or any other columns for that matter but it should be random and not ordered.

Ofcouse the total no. of columns and rows are fixed in that file and 'n' the number of random columns required can vary.
# 7  
Old 04-24-2011
Ruby(1.9.1+)
Code:
f=open("file")
numcols=f.readline.split.size
cols=(1..numcols).to_a.sample(4)
while not f.eof?
    line=f.gets.split
    cols.each{|x| printf "%s " , line[x-1]}
    puts
end
f.close

This User Gave Thanks to kurumi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Which Product to Choose?

Okay, I have an Asus A8NSLI board with an Athlon 64 and I dunno, maybe 8gig Ram and Windows has crashed for the last time so I've finally had enough and I'll make it a Unix machine. I have a new 1Tera drive and I'm all set to go. Which brand of Unix/Linux can you advise me to go for? The... (3 Replies)
Discussion started by: abrogard
3 Replies

2. Shell Programming and Scripting

Script that will random choose an IP address

Hi, I need to write a bash script that will random choose and login into these below ip addresses. 192.168.116.130 192.168.116.131 192.168.116.132 192.168.116.133 I'm new into scripting and I need to enhance my logic. Below is what i did ... (4 Replies)
Discussion started by: Milon
4 Replies

3. Shell Programming and Scripting

Selecting random columns from large dataset in UNIX

Dear folks I have a large data set which contains 400K columns. I decide to select 50K determined columns from the whole 400K columns. Is there any command in unix which could do this process for me? I need to also mention that I store all of the columns id in one file which may help to select... (5 Replies)
Discussion started by: sajmar
5 Replies

4. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

5. Shell Programming and Scripting

choosing random columns from a file

Hello, I want to choose random columns from big file. for example: My file contain around 21000 columns and I want to randomly extract 4000 columns from this file. Anybody has a solution (may be one liner or a function in perl or awk) for this? Thanks, R (2 Replies)
Discussion started by: ryan9011
2 Replies

6. What is on Your Mind?

Which Tablet to Choose?

Currently in the process of looking for a tablet. Which one is best? Thanks Benjamin Mauerberger (9 Replies)
Discussion started by: hlinks12
9 Replies

7. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

8. Shell Programming and Scripting

choose y or n

Hi, I have written a choice based shell script some thing like this: if (y) execute code .... fi else if(n) terminating the problem with the above scripting is it will work as far as the options are y or n. but i want to reiterate the same code when the user inputs something else... (1 Reply)
Discussion started by: sunrexstar
1 Replies

9. Shell Programming and Scripting

choose random text between constant string.. using awk?

Hallo I have maybe a little bit advanced request.... I need to choose one random part betwen %.... so i have this.. % text1 text1 text1 text1 text1 text1 text1 text1 text1 % text2 text2 text2 text2 text2 % text3 text3 text3 tetx3 % this choose text between % awk ' /%/... (8 Replies)
Discussion started by: sandwich
8 Replies
Login or Register to Ask a Question