ok, lets say if I need 2 random columns it can be any two e.g.
Code:
1,6 1,10
2,6 2,10
3,6 3,10
4,6 4,10
and if I say I need 3 random columns, it can be any three, but what is important is it should be random, may be by using some function for choosing columns randomly
What languages do you have available? I'm pondering a solution in C.
---------- Post updated at 01:27 PM ---------- Previous update was at 12:20 PM ----------
Code:
/**
* rc.c picks random columns from whitespace-separated input from stdin.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
const char ofs=' '; // output field separator
const char *fs=" \r\n\t"; // input field seperator
int main(int argc, char *argv[])
{
size_t size; // buf[] size in bytes
char *buf=malloc(size=65536L);
int pos=0; // How much's been read into buf[]
int coln=0, colmax; // How many columns read, max columns in cols[]
char **cols=malloc(sizeof(char *) * (colmax=512L));
int choose=1; // How many columns to choose
if(argc != 2)
{
fprintf(stderr, "Usage: %s columns < datafile\n",argv[0]);
return(1);
}
if((sscanf(argv[1], "%d", &choose) != 1) || (choose <= 0))
{
fprintf(stderr, "Bad count '%s'\n", argv[1]);
return(1);
}
srand(time(NULL)^getpid()); // Make results random
// Read until end of file
while(fgets(buf+pos, size-pos, stdin) != NULL)
{
int c;
char *tok;
pos += strlen(buf+pos);// Find the end of line
if(pos <= 0) continue; // Don't bother checking empty line
// Check the end of line for \n
if(buf[pos-1] != '\n')
{ // Didn't get entire line, make buffer bigger
// then get the rest
buf=realloc(buf, size += size>>1);
continue;
}
// Break into columns across whitespace
tok=strtok(buf, fs);
do
{
// Check if we have enough room for columns.
// Add more if necessary.
if(colmax <= coln)
cols=realloc(cols, sizeof(char *)*
(colmax+=(colmax>>1)));
cols[coln++]=tok;
tok=strtok(NULL, fs);
} while(tok != NULL);
for(c=0; (c<choose)&&(coln>0); c++)
{
int m=rand()%coln;
char *pick=cols[m];
cols[m]=cols[--coln]; // Remove from list
if(c != 0) putc(ofs, stdout);
fputs(pick, stdout);
}
putc('\n', stdout);
// Reset everything for next line
coln=0; pos=0;
}
return(0);
}
should handle very large lines and thousands of columns without problem.
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)
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)
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)
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)
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)
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)
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)
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)