Putting values into order in a field using awk

Hi,

I am using UBUNTU 12.04.

I have a dataset as follows:

Column#1 Column#2 Column#3 .... Column#50
                     1             154878 
                     1             145145
                     2             189565 
                     2             454121
                     .                     .
                     .                     .
                     .                    .
                   22              124579    

My questions:

  1. I want to divide all the values in column#3 by 1000000 and print all the file. I know doing it by writing #awk'{print $1,$2,$3/1000000,$4....$50}' but as it takes too much time, I wanted to see whether there is any more efficient way?

  2. I want to order all the values in column#3 according to column#2, that is first it puts all the values in order for all 1s in column 2, then do it for all the 2s and so on. So, the output looks like this:

Column#1 Column#2 Column#3 .... Column#50
                     1             145145
                     1             154878 
                     2             145412
                     2             189565 
                                 
                     .                     .
                     .                     .
                     .                    .
                   22              124579    

Thank you very much.

try:

awk '{$3/=1000000; print}' infile | sort -n

OR

awk '{$3/=1000000}1' infile | sort -n

Thank you, although it doesn't sort the third column.

Using this command, how I can keep the spacing between my fields? I have used this command but it doesn' work properly.

#awk 'BEGIN{OFS=ORS=""}{$3/=1000000 print\n"}' infile

use this

awk '{$3/=1000000}1' infile | sort -nk3

OFS="" Thats why it is not showing any spacing. Use OFS=" " or by default it is space so no need to define OFS.
And after \n you need " there.

Works perfectly, thanks