random number

How can I choose randomly the row numbers of my file in awk?

Like this?

awk -v totrecs="$(wc -l < file)" 'BEGIN{srand()
ranrec=int(1+totrecs*rand())}
NR==ranrec{print;exit}' file
1 Like

Not all awks support srand(). If you are on Solaris use nawk, not awk.

I have a file with 441846 fields. I have another file that I want to assign its columns to my file with 441846 fields. My code is this:

BEGIN{
OFS=ORS=""}
{
if (FILENAME==ARGV[1]){SELSWI[$1]=FNR; nind=FNR}
if (FILENAME==ARGV[2]){
if ($1 in SELSWI){sel[SELSWI[$1]]=$1}
}
{print sel[ID_num[$1]], ""..........................

here I don't know how I should say that awk you must print the first field that I have changed plus all the others fields which are 441846.

I am a beginner with programming and awk, would be great if you could help me. Thanks.

Please give us sample input from both files, and sample desired output.